java-顺序打印多线程;数组最大值与第一位交换、最小值与最后一位交换


package Test5_4;

/**
	3.(编程题) 
	要求使用线程同步与等待机制实现如下打印 
	*Thread-0#Thread-1@Thread-2 
	*Thread-0#Thread-1@Thread-2*Thread-0#Thread-1@Thread-2 
	*Thread-0#Thread-1@Thread-2*Thread-0#Thread-1@Thread-2*Thread-0#Thread-1@Thread-2 
	... 
	循环1000次 
*/

class PrintThread{
	private static int flag=0;
    private int count=0;
    public PrintThread(int count) {
    	this.count=count;
    }
    public void print() {
		Object lock = new Object();
		Thread aThread = new Thread(new Runnable() {
			@Override
			public void run() {
				for(int i=0;i

java-顺序打印多线程;数组最大值与第一位交换、最小值与最后一位交换_第1张图片


package Test5_4;

/**	
	题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。 
*/
public class Test2 {
	public static void main(String[] args) {
		int[] array= {0,1,2,3,4,5,6,7,8,9};
		for (int i : array) {
			System.out.print(i+" ");
		}
		System.out.println();
		changeMaxMin(array);
		for (int i : array) {
			System.out.print(i+" ");
		}
	}
	
	public static void swap(int[] array, int x,int y) {
		int tmp = array[x];
		array[x] = array[y];
		array[y] = tmp;
	}
	
	public static void changeMaxMin(int[] array) {
		int max=0, min=0;
		for(int i=0;iarray[i]) {
				min=i;
			}
		}	
		swap(array, 0, max);
		if(min==0) {	//若min的下标就是0,则0的元素已被max交换,则min的值应变为max
			min=max;
		}
		swap(array, array.length-1, min);
	}
}
java-顺序打印多线程;数组最大值与第一位交换、最小值与最后一位交换_第2张图片

你可能感兴趣的:(Java)