线程切换+同步

面试题:子线程循环10次,接着主线程循环100,接着又回到子线程循环10次,接着再回到主线程又循环100,如此循环50次,请写出程序。

使用synchronized +wait()+notify()

package threadExchangeExecute;
/**
 * 子线程循环10次,接着主线程循环100,
 * 接着又回到子线程循环10次,
 * 接着再回到主线程又循环100,如此循环50次
 * synchronized
 * @author q
 *
 */
public class MainThread {
	static int j=0;
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		MainThread mainThread=new MainThread();
		Thread coreThread=mainThread.new CoreThread();
		coreThread.setName(coreThread.getClass().getName());
		Thread subThread=mainThread.new SubThread();
		subThread.setName(subThread.getClass().getName());
		coreThread.start();
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		subThread.start();
	}
	class CoreThread extends Thread{
		public void run() {
			synchronized (MainThread.class) {
				while (j < 50) {
					for (int i = 0; i < 200; i++) {
						if (i % 100 == 0) {
							//第1次和第101次执行之前先处于等待状态,让子线程先执行
							//由子线程执行完第10次和第20次之后notify主线程来执行
							MainThread.class.notify();
							try {
								MainThread.class.wait();
							} catch (InterruptedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
						show(i);
						if (i == 199) {
							//第200次执行完后,通知子线程开始下一次循环
							System.out.println("大循环execute 第" + (j + 1) + "次");
							j++;
						}
					}
				}
			}
		}
	}
	class SubThread extends Thread{
		public void run() {
			synchronized (MainThread.class) {
				while (j < 50) {
					for (int i = 0; i < 20; i++) {
						if (i == 10) {
							//执行完第10次之后,通知主线程可以开始排队竞争资源了,自己变为等待状态
							MainThread.class.notify();
							try {
								MainThread.class.wait();
							} catch (InterruptedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
						show(i);
						if (i == 19) {
							//执行完第20次之后,通知主线程可以开始排队竞争资源了,自己变为等待状态
							MainThread.class.notify();
							try {
								MainThread.class.wait();
							} catch (InterruptedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}

						}
					}

				}
			}
		}
	}
	public void show(int i)
	{
		System.out.println(Thread.currentThread().getName() + "execute 第" + (i + 1) + "次");
	}
}

使用ReentrantLock和Condition

package threadExchangeExecute;

/**
 * 子线程循环10次,接着主线程循环100,
 * 接着又回到子线程循环10次,
 * 接着再回到主线程又循环100,如此循环50次
 * ReentrantLock And Condition
 * @author q
 *
 */
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

import threadExchangeExecute.MainThread.CoreThread;
import threadExchangeExecute.MainThread.SubThread;

public class ByLockCondition {
	ReentrantLock lock = new ReentrantLock();
	Condition main = lock.newCondition();
	Condition sub = lock.newCondition();
	int j = 0;

	public static void main(String[] arg) {
		ByLockCondition mainThread = new ByLockCondition();
		Thread coreThread = mainThread.new MainThread();
		coreThread.setName(coreThread.getClass().getName());
		Thread subThread = mainThread.new SubThread();
		subThread.setName(subThread.getClass().getName());
		coreThread.start();
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		subThread.start();
	}

	class MainThread extends Thread {
		public void run() {
			while (j < 50) {
				lock.lock();
				try {
					for (int i = 0; i < 200; i++) {
						if (i % 100 == 0) {
							sub.signalAll();
							main.await();
						}
						show(i);
						if (i == 199) {
							System.out.println("大循环execute 第" + (j + 1) + "次");
							j++;
						}
					}
				}catch (Exception e) {
				} finally {
					lock.unlock();
				}

			}
		}
	}
	
	class SubThread extends Thread {
		public void run() {
			while (j < 50) {
				lock.lock();		
				try {
					for (int i = 0; i < 20; i++) {
						if (i == 10) {
							main.signalAll();
							sub.await();
						}
						show(i);
						if (i == 19) {
							main.signalAll();
							sub.await();
						}
					}
				} catch (Exception e) {
				} finally {
					lock.unlock();
				}
			}
		}
	}

	public void show(int i) {
		System.out.println(Thread.currentThread().getName() + " execute 第" + (i + 1) + "次");
	}

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值