多线程练习-synochronized-notifywait-lockcondition

xiaoxiao2021-02-27  289

子线程循环 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的锁 } } } }

转载请注明原文地址: https://www.6miu.com/read-11173.html

最新回复(0)