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<count;) {
					synchronized (lock) {
						if(flag%3==0&&"Thread-0".equals(Thread.currentThread().getName())) {
							System.out.print("*Thread-0");
							flag++;
							lock.notifyAll();
							i++;
						}
						else {
							try {
								lock.wait();
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						}
					}
				}
			}
		});
		Thread bThread = new Thread(new Runnable() {
			@Override
			public void run() {
				for(int i=0;i<count;) {
					synchronized (lock) {
						if(flag%3==1&&"Thread-1".equals(Thread.currentThread().getName())) {
							System.out.print("#Thread-1");
							flag++;
							lock.notifyAll();
							i++;
						}
						else {
							try {
								lock.wait();
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						}
					}
				}
			}
		});
		Thread cThread = new Thread(new Runnable() {
			@Override
			public void run() {
				for(int i=0;i<count;) {
					synchronized (lock) {
						if(flag%3==2&&"Thread-2".equals(Thread.currentThread().getName())) {
							System.out.print("@Thread-2\r\n");
							flag++;
							lock.notifyAll();
							i++;
						}
						else {
							try {
								lock.wait();
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						}
					}
				}
			}
		});
		aThread.start();
		bThread.start();
		cThread.start();
    }
}

public class Test {

	public static void main(String[] args) throws InterruptedException {
		PrintThread mt = new PrintThread(3);
		mt.print();
	}
}


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;i<array.length;i++) {
			if(array[max]<array[i]) {
				max=i;
			}
			if(array[min]>array[i]) {
				min=i;
			}
		}	
		swap(array, 0, max);
		if(min==0) {	//若min的下标就是0,则0的元素已被max交换,则min的值应变为max
			min=max;
		}
		swap(array, array.length-1, min);
	}
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Victor.Chang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值