多线程练习-synochronized-notify/wait-lock/condition

子线程循环 10 次,接着主线程循环 100,接着又回到子线程循环 10 次,

* 接着再回到主线程又循环 100,如此循环 50次,请写出程序


注意:

在Eclipse中:run-->Run Configurations-->Common-->Standard input and output (可能不同版本的Eclipse位置会有所不同)

File选择你想要保存的文件(比如:E:\result.txt)。

这样,控制台输出的所有内容都会保存到文本result.txt中


第一次代码:用flag做信号灯,决定该子线程还是主线程跑;

用wait和notify控制交流

package 面试题;

public class Mianshiti {

	static boolean flag = false;//真为主线程在跑,假为子线程在跑
	
	/*子线程循环 10 次,接着主线程循环 100,接着又回到子线程循环 10 次, 
	 * 接着再回到主线程又循环 100,如此循环 50次,请写出程*/	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Mianshiti mianshiti = new Mianshiti(); 
		Mainthread mainthread = new Mainthread(mianshiti);
		Subthread subthread = new Subthread(mianshiti);
		Thread mainthr = new Thread(mainthread);
		Thread subthr = new Thread(subthread);
		subthr.start();
		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		mainthr.start();
	}

	static class Mainthread implements Runnable{
		Object object;
		
		public Mainthread(Object object) {
			// TODO Auto-generated constructor stub
			this.object = object;
		}
		
		@Override
		public void run() {
			// TODO Auto-generated method stub
			for (int i = 0; i < 50; i++) {
				synchronized (object) {
					if (!flag) {//为假,说明子线程在跑,等他跑完
						try {
							object.wait();
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					for (int j = 0; j < 20; j++) {
						System.out.println("mainthread执行第:"+i+"次中:第"+j+"遍");
					}
					flag=false;//让给子线程跑
					object.notify();
				}
			}
		}
	}
	
	static class Subthread implements Runnable{
		
		Object object;
		
		public Subthread(Object object) {
			// TODO Auto-generated constructor stub
			this.object = object;
		}
		
		@Override
		public void run() {
			// TODO Auto-generated method stub
			for (int i = 0; i < 50; i++) {
				synchronized (object) {
					if (flag) {//为真,主线程在跑,等他跑完
						try {
							object.wait();
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					for (int j = 0; j < 10; j++) {
						System.out.println("subthread执行第:"+i+"次中:第"+j+"遍");
					}
					flag = true;//该让给主线程跑
					object.notify();
				}
			}
		}
	}
}

但是两个类的内容其实一样,所以用一个类装两个方法就可以了;

package 面试题;

public class Mianshiti2 {

	static boolean flag = false;
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Printsign printsign = new Printsign();
		Thread subthread = new Thread(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				for (int i = 0; i <= 50; i++) {
					printsign.subthread(i);
				}
			}
		});
		subthread.start();//开始子线程
		
		for (int i = 0; i < 50; i++) {//把main线程作为父线程
			printsign.mainthread(i);
		}
	}

	static class Printsign{
		
		public synchronized void subthread(int i) {
			if (flag) {
				try {
					this.wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			for (int j = 0; j < 10; j++) {
				System.out.println("subthread执行第:"+i+"次中:第"+j+"遍");
			}
			flag=true;
			this.notify();
		}
		
		public synchronized void mainthread(int i) {
			if (!flag) {
				try {
					this.wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			for (int j = 0; j < 20; j++) {
				System.out.println("mainthread执行第:"+i+"次中:第"+j+"遍");
			}
			flag=false;
			this.notify();
		}
	}
}

用condition和lock控制通信:

package 面试题;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/*用condition控制线程通信*/
public class Mianshiti3 {

	
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Printsing printsing = new Printsing();
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				for (int j = 0; j < 50; j++) {
					printsing.subprint(j);
				}
			}
		}).start();
		
		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		for (int i = 0; i < 50; i++) {
			printsing.mainprint(i);
		}
	}

	static class Printsing{
		private final Lock lock = new ReentrantLock();
		private final Condition condition = lock.newCondition();
		boolean flag = false;
		public void subprint(int i) {
			lock.lock();
			try {
				if (flag) {
					try {
						condition.await();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				for (int j = 0; j < 10; j++) {
					System.out.println("subthread执行第:"+i+"次中:第"+j+"遍");
				}
				flag = true;
				condition.signal();
			} catch (Exception e) {
				// TODO: handle exception
			} finally {
				lock.unlock();//释放Lock的锁
			}
		}
		
		public void mainprint(int i) {
			lock.lock();
			try {
				if (!flag) {
					try {
						condition.await();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				for (int j = 0; j < 20; j++) {
					System.out.println("mainthread执行第:"+i+"次中:第"+j+"遍");
				}
				flag = false;
				condition.signal();
			} catch (Exception e) {
				// TODO: handle exception
			} finally {
				lock.unlock();//释放Lock的锁
			}
		}
	}
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

obession

觉得有用可以打赏咖啡一杯~

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

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

打赏作者

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

抵扣说明:

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

余额充值