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

来源:互联网 发布:java replace用法 编辑:程序博客网 时间:2024/05/04 10:42
子线程循环 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 stubMianshiti 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 blocke.printStackTrace();}mainthr.start();}static class Mainthread implements Runnable{Object object;public Mainthread(Object object) {// TODO Auto-generated constructor stubthis.object = object;}@Overridepublic void run() {// TODO Auto-generated method stubfor (int i = 0; i < 50; i++) {synchronized (object) {if (!flag) {//为假,说明子线程在跑,等他跑完try {object.wait();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.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 stubthis.object = object;}@Overridepublic void run() {// TODO Auto-generated method stubfor (int i = 0; i < 50; i++) {synchronized (object) {if (flag) {//为真,主线程在跑,等他跑完try {object.wait();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.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 stubPrintsign printsign = new Printsign();Thread subthread = new Thread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubfor (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 blocke.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 blocke.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 stubPrintsing printsing = new Printsing();new Thread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubfor (int j = 0; j < 50; j++) {printsing.subprint(j);}}}).start();try {Thread.sleep(100);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.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 blocke.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 blocke.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
原创粉丝点击