java线程协作,经典生产者/消费者模式(一、synchronized互斥)
2016-06-20 22:24
531 查看
java多线程协作,最经典的就是生产者/消费者模式,,比如,一个盘子(缓冲区),假如这个盘子最多只能放一个苹果。现在如果我要从盘里拿出一个苹果,首先要保证盘里有苹果,如果没有,就要等到放入了才能拿;同样,我如果要放一个苹果到盘子里,就要保证盘子里面现在没有苹果,如果有,就要等拿走了又才能放。下面贴上实现放入苹果/拿出苹果这个过程的线程代码:
以上这个列子就是经典的生产者/消费者模式,如果有不正确的地方,请看见的大神指出来,以免误导了后面学习的人。
下面,在贴一段经典面试题的代码:大致需求如下,开启ABC三个线程,A打印A,B打印B,C打印C,ABC三个线程同时启动,顺序输入ABC,如此循环10次。
/**
* 生产者/消费者 :顺序打印ABCD
* @author 李小拐
* 2016年6月20日21:00:36
*/
public class PrintABCD {
/**
* @param args
*/
public static void main(String[] args) {
//一定是在这里new一个对象,不能再线程里面new,否则多个线程就不是操作的同一个内存对象
PrintABCD abcd=new PrintABCD();
for (int i = 0; i < 10; i++) {
(new printA(abcd)).start();
(new printB(abcd)).start();
(new printC(abcd)).start();
(new printD(abcd)).start();
}
}
private static int who=1;//用变量who来控制要打印的值;1,2,3,4分别代表A,B,C,D
public synchronized void printA() throws InterruptedException {
while (who!=1) {
this.wait();//不等于1就等待,等于1的时候才执行
}
System.out.println("A");
who=2;//A的下一状态B
this.notifyAll();
}
public synchronized void printB() throws InterruptedException {
while (who!=2) {
this.wait();
}
System.out.println("--B");
who=3;//B的下一状态C
this.notifyAll();
}
public synchronized void printC() throws InterruptedException {
while (who!=3) {
this.wait();
}
System.out.println("----C");
who=4;//C的下一状态D
this.notifyAll();
}
public synchronized void printD() throws InterruptedException {
while (who!=4) {
this.wait();
}
System.out.println("------D");
who=1;//D的下一状态A
this.notifyAll();
}
}
//四个线程,分别打印ABCD
class printA extends Thread{
PrintABCD p;
printA(PrintABCD p){
this.p=p;
}
@Override
public void run() {
try {
p.printA();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class printB extends Thread{
PrintABCD p;
printB(PrintABCD p){
this.p=p;
}
@Override
public void run() {
try {
p.printB();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class printC extends Thread{
PrintABCD p;
printC(PrintABCD p){
this.p=p;
}
@Override
public void run() {
try {
p.printC();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class printD extends Thread{
PrintABCD p;
printD(PrintABCD p){
this.p=p;
}
@Override
public void run() {
try {
p.printD();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
这里打印了ABCD,其实和ABC是一样的,只是demo里面多写了一个。synchronized关键字保证了线程的互斥。JDK1.5以前基本都使用synchronized关键字进行线程间协作,JDK1.5以后提供了更高级的通信方式,我将在下一篇文章贴出demo代码。
最后一句,学无止境,文章若有不足之处,请大神们及时指正,,纠正了我,也及时避免误导后人
/** * java线程协作,生产者/消费者 * @author 李小拐 * 2016年6月20日20:36:56 */ public class Plate { private static int plate=0;//声明一个变量,代表盘子,可以放入和拿出 //放入一个苹果,使用synchronized关键字 public synchronized void putApple() throws InterruptedException { while (plate==1) { this.wait();//如果有苹果,不能再放了,就等待 } System.out.println("放入apple"); plate=1; this.notifyAll();//放入后,唤醒等待的线程 } //拿出一个苹果,使用synchronized关键字 public synchronized void getApple() throws InterruptedException { while (plate==0) { this.wait();//如果盘子为空,则不能取,等待放入苹果才能取 } System.out.println("拿出苹果"); plate=0; this.notifyAll();//取出后,唤醒等待线程 } public static void main(String[] args) { Plate plate=new Plate(); for (int i = 0; i < 200; i++) {//put和get各执行20次 new getApple(plate).start(); new putApple(plate).start(); } } } //拿出苹果的线程 class getApple extends Thread{ private Plate plate; public getApple(Plate plate) { this.plate=plate; } @Override public void run() { try { plate.getApple(); } catch (InterruptedException e) { e.printStackTrace(); } } } //放入苹果的线程 class putApple extends Thread{ private Plate plate; public putApple(Plate plate) { this.plate=plate; } @Override public void run() { try { plate.putApple(); } catch (InterruptedException e) { e.printStackTrace(); } } }
以上这个列子就是经典的生产者/消费者模式,如果有不正确的地方,请看见的大神指出来,以免误导了后面学习的人。
下面,在贴一段经典面试题的代码:大致需求如下,开启ABC三个线程,A打印A,B打印B,C打印C,ABC三个线程同时启动,顺序输入ABC,如此循环10次。
/**
* 生产者/消费者 :顺序打印ABCD
* @author 李小拐
* 2016年6月20日21:00:36
*/
public class PrintABCD {
/**
* @param args
*/
public static void main(String[] args) {
//一定是在这里new一个对象,不能再线程里面new,否则多个线程就不是操作的同一个内存对象
PrintABCD abcd=new PrintABCD();
for (int i = 0; i < 10; i++) {
(new printA(abcd)).start();
(new printB(abcd)).start();
(new printC(abcd)).start();
(new printD(abcd)).start();
}
}
private static int who=1;//用变量who来控制要打印的值;1,2,3,4分别代表A,B,C,D
public synchronized void printA() throws InterruptedException {
while (who!=1) {
this.wait();//不等于1就等待,等于1的时候才执行
}
System.out.println("A");
who=2;//A的下一状态B
this.notifyAll();
}
public synchronized void printB() throws InterruptedException {
while (who!=2) {
this.wait();
}
System.out.println("--B");
who=3;//B的下一状态C
this.notifyAll();
}
public synchronized void printC() throws InterruptedException {
while (who!=3) {
this.wait();
}
System.out.println("----C");
who=4;//C的下一状态D
this.notifyAll();
}
public synchronized void printD() throws InterruptedException {
while (who!=4) {
this.wait();
}
System.out.println("------D");
who=1;//D的下一状态A
this.notifyAll();
}
}
//四个线程,分别打印ABCD
class printA extends Thread{
PrintABCD p;
printA(PrintABCD p){
this.p=p;
}
@Override
public void run() {
try {
p.printA();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class printB extends Thread{
PrintABCD p;
printB(PrintABCD p){
this.p=p;
}
@Override
public void run() {
try {
p.printB();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class printC extends Thread{
PrintABCD p;
printC(PrintABCD p){
this.p=p;
}
@Override
public void run() {
try {
p.printC();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class printD extends Thread{
PrintABCD p;
printD(PrintABCD p){
this.p=p;
}
@Override
public void run() {
try {
p.printD();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
这里打印了ABCD,其实和ABC是一样的,只是demo里面多写了一个。synchronized关键字保证了线程的互斥。JDK1.5以前基本都使用synchronized关键字进行线程间协作,JDK1.5以后提供了更高级的通信方式,我将在下一篇文章贴出demo代码。
最后一句,学无止境,文章若有不足之处,请大神们及时指正,,纠正了我,也及时避免误导后人
相关文章推荐
- Python3写爬虫(四)多线程实现数据爬取
- C#实现多线程的同步方法实例分析
- 浅谈chuck-lua中的多线程
- C#简单多线程同步和优先权用法实例
- C#多线程学习之(四)使用线程池进行多线程的自动管理
- C#多线程编程中的锁系统(三)
- 解析C#多线程编程中异步多线程的实现及线程池的使用
- C#多线程学习之(六)互斥对象用法实例
- 基于一个应用程序多线程误用的分析详解
- C#多线程学习之(三)生产者和消费者用法分析
- C#多线程学习之(一)多线程的相关概念分析
- C#多线程之Thread中Thread.IsAlive属性用法分析
- 分享我在工作中遇到的多线程下导致RCW无法释放的问题
- C#多线程编程之使用ReaderWriterLock类实现多用户读与单用户写同步的方法
- C#多线程传递参数及任务用法示例
- C#控制台下测试多线程的方法
- 21天学习android开发教程之SurfaceView与多线程的混搭
- Ruby 多线程的潜力和弱点分析
- C#中WPF使用多线程调用窗体组件的方法
- C#如何对多线程、多任务管理(demo)