代码如下:
class Info{
private String name=null; //名称
private String content=null; //内容
private boolean flag=false; //设置标志位
public synchronized void set(String name,String content){
if (!flag){
try{
super.wait();
}catch(InterruptedException e){
e.printStackTrace();
}
}
try{
Thread.sleep(2000); //延迟
}catch(InterruptedException e){
e.printStackTrace();
}
this.setName(name); //设置名称
this.setContent(content); //设置内容
flag=false;
super.notify();
}
public synchronized void get( ){
if (flag){
try{
super.wait();
}catch(InterruptedException e){
e.printStackTrace();
}
}
try{
Thread.sleep(2000); //延迟
}catch(InterruptedException e){
e.printStackTrace();
}
System.out.println(this.getName()+"-->"+this.getContent());
flag=true;
super.notify();
}
public void setName(String name){ //设置名称
this.name=name;
}
public void setContent(String content){ //设置内容
this.content=content;
}
public String getName(){ //取得名称
return this.name;
}
public String getContent(){ //取得内容
return this.content;
}
}
class Producer implements Runnable{ //继承Runnable接口
private Info info=null; //声明info
public Producer(Info info){ //通过构造方法设置info
this.info=info;
}
public void run(){ //覆写run()方法
boolean flag=false; //声明flag
for (int i=0;i<50 ;i++ ){
if (flag){
this.info.set("徐彦","程序员");
flag=false;
}
else{
this.info.set("希哲","教师");
flag=true;
}
}
} }
class Consumer implements Runnable{ //继承Runnable接口
private Info info=null; //声明info
public Consumer(Info info){
this.info=info;
}
public void run(){ //覆写run()
for (int i=0;i<50 ;i++ ){
this.info.get();
}
}
}
public class ThreadCaseDemo03{
public static void main(String args[]){
Info info=new Info(); //实例化info
Producer pro=new Producer(info); //生产者
Consumer con=new Consumer(info); //消费者
new Thread(pro).start();
new Thread(con).start();
}
}
小弟一直困惑程序的执行流程,希望通过内存图看个明白,各位大侠谢谢了啊!!!
class Info{
private String name=null; //名称
private String content=null; //内容
private boolean flag=false; //设置标志位
public synchronized void set(String name,String content){
if (!flag){
try{
super.wait();
}catch(InterruptedException e){
e.printStackTrace();
}
}
try{
Thread.sleep(2000); //延迟
}catch(InterruptedException e){
e.printStackTrace();
}
this.setName(name); //设置名称
this.setContent(content); //设置内容
flag=false;
super.notify();
}
public synchronized void get( ){
if (flag){
try{
super.wait();
}catch(InterruptedException e){
e.printStackTrace();
}
}
try{
Thread.sleep(2000); //延迟
}catch(InterruptedException e){
e.printStackTrace();
}
System.out.println(this.getName()+"-->"+this.getContent());
flag=true;
super.notify();
}
public void setName(String name){ //设置名称
this.name=name;
}
public void setContent(String content){ //设置内容
this.content=content;
}
public String getName(){ //取得名称
return this.name;
}
public String getContent(){ //取得内容
return this.content;
}
}
class Producer implements Runnable{ //继承Runnable接口
private Info info=null; //声明info
public Producer(Info info){ //通过构造方法设置info
this.info=info;
}
public void run(){ //覆写run()方法
boolean flag=false; //声明flag
for (int i=0;i<50 ;i++ ){
if (flag){
this.info.set("徐彦","程序员");
flag=false;
}
else{
this.info.set("希哲","教师");
flag=true;
}
}
} }
class Consumer implements Runnable{ //继承Runnable接口
private Info info=null; //声明info
public Consumer(Info info){
this.info=info;
}
public void run(){ //覆写run()
for (int i=0;i<50 ;i++ ){
this.info.get();
}
}
}
public class ThreadCaseDemo03{
public static void main(String args[]){
Info info=new Info(); //实例化info
Producer pro=new Producer(info); //生产者
Consumer con=new Consumer(info); //消费者
new Thread(pro).start();
new Thread(con).start();
}
}
小弟一直困惑程序的执行流程,希望通过内存图看个明白,各位大侠谢谢了啊!!!
Java中每一个对象都有一把隐式锁(只有一把)。拿Info info=new Info()来说吧:info对象就有一把隐锁,当线程A调用info.set()方法的时候,线程B是无法调用info.get()方法的。因为这两个方法都被synchronized声明了,也就是被同一把锁保护起来了。好了,现在来看看上面的代码,基本运行情况是这样的:
阶段1
new Thread(pro).start();
new Thread(con).start();
生产者线程先运行,当他开始调用info.set("希哲","教师")的同时,消费者线程也准备开始调用info.get()了。但是注意:由于生产者先运行info对象的set方法,也就先获得了info对象的唯一的一把锁。这个时候消费者线程想要调用info.get()的时候就因没有锁而被阻塞了。阶段2
这个时候,生产者开始运行info.set()方法了,但是很遗憾,这个线程一开始就运行进了这段代码:
if (flag){
try{
super.wait();
}catch(InterruptedException e){
e.printStackTrace();
}
}
没办法,还没有将"希哲","教师"赋值给name和content变量,生产者线程就进入了阻塞等待状态,并放弃了对象的唯一的锁。此时的info对象中的name和content变量仍然是null。
此时,没有锁而被阻塞的消费者线程就可以得到锁而开始运行info.get()方法了。运行的时候由于flag=false,因此消费者线程不会被wait(),sleep 2秒之后就开始打印this.getName()+"-->"+this.getContent()了。
这个时候打印:结果只能是null->>null还有一个问题是至于为什么后面是4秒运行打印一次,很简单。
这段代码是生产者线程和消费者线程轮流运行各自的代码,我们拿出一个循环单元开始分析。首先,消费者wait()时,生产者获得锁进入set()方法,开始先等待2秒后赋值一个(name,content)对。
然后,生产者调用notify()方法,激活阻塞的消费者并自己放弃锁。
接着,消费者开始获得锁并进入get()方法,开始先等待2秒后打印生产者赋值的(name,content)对。很显然,两次打印之间各了两个线程均等待的2秒钟,自然是4秒一次打印了。