多线程练习-synochronized-notify/wait-lock/condition
2017-08-30 21:26
459 查看
子线程循环 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的锁
}
}
}
}
* 接着再回到主线程又循环 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的锁
}
}
}
}
相关文章推荐
- Java多线程实现生产者消费者程序(Wait,Notify实现和Lock,Condition实现)
- 多线程通讯(wait、notify、Lock、ThreadLocal)
- Lock+Condition 相对于 wait+notify 的一个优势案例分析
- 黑马程序员:线程间通信介绍:wait()、notify()、Lock、Condition等介绍
- synchronized VS Lock, wait-notify VS Condition
- java线程通讯——使用Lock和Condition代替synchronized 和 wait, notify notifyAll()
- java线程通讯——使用Lock和Condition代替synchronized 和 wait, notify notifyAll()
- java线程通讯——使用Lock和Condition代替synchronized 和 wait, notify notifyAll()
- 多线程之线程间协作的两种方式:wait、notify、notifyAll和Condition
- java线程通讯——使用Lock和Condition代替synchronized 和 wait, notify notifyAll()
- 练习:利用多线程 按照顺序将 ABC 打印十遍 ,体会 lock 和 condition 的用法
- Lock的lock/unlock, condition的await/singal 和 Object的wait/notify 的区别 - 永远是 ...
- 4.1.4ReentrantLock与Condition代替wait/notify
- java并发包中的Condition和Lock 取代Synchronized、wait、notify/notifyAll实现线程的同步与互斥
- java多线程学习之线程通信-wait()等待 notify()唤醒
- 深入理解Java多线程中的wait(),notify()和sleep()
- java 并发编程:线程间的协作 wait()+notify()/notifyall() 和 Condition
- Java多线程核心技术笔记-wait()和notify()
- 多线程中wait(),notify(),notifyAll()
- Java并发编程:线程间协作的两种方式:wait、notify、notifyAll和Condition