Java-多线程的应用
2013-07-23 09:29
288 查看
class Info //主信息类
{
private String name="Aaron";
private String content="Ghost";
private boolean flag=false; //设置标志位
public void setname(String name)
{
this.name=name;
}
public String getname()
{
return this.name;
}
public void setcontent(String content)
{
this.content=content;
}
public String getcontent()
{
return this.content;
}
public synchronized void set(String name,String content)
{
if(!flag)
{
try
{
super.wait();//等待消费者取出
}catch(InterruptedException e)
{
e.printStackTrace();
}
}
this.setname(name);
try{
Thread.sleep(300);
}catch(InterruptedException e)
{
e.printStackTrace();
}
this.setcontent(content);
flag=false;//重置标志位,表示可以取走
super.notify();//唤醒等待线程
}
public synchronized void get()
{
if(flag)
{
try
{
super.wait();
}catch(InterruptedException e)
{
e.printStackTrace();
}
}
try{
Thread.sleep(90);
}catch(InterruptedException e)
{
e.printStackTrace();
}
System.out.println("姓名:"+this.getname()+"内容:"+this.getcontent());
flag=true; //修改标志位,表示可以恢复生产
super.notify(); //唤醒等待线程
}
}
class producer implements Runnable
{
private Info info=null;
public producer(Info info)
{
this.info=info;
}
public void run() //run方法的覆写
{
boolean flag=false;
for(int i=0;i<20;i++)
{
if(flag)
{
this.info.set("Aaron", "Ghost");
flag=false;
}
else
{
this.info.set("husiyun", "goal");
flag=true;
}
}
}
}
class consumer implements Runnable
{
private Info info=null;
public consumer(Info info)
{
this.info=info;
}
public void run() //run方法的覆写
{
for(int i=0;i<20;i++)
{
try
{
Thread.sleep(100);
}catch(InterruptedException e)
{
e.printStackTrace();
}
this.info.get();
}
}
}
public class test70 {
public static void main(String args[])
{
Info i=new Info(); //先实例化
producer pro=new producer(i);
consumer con=new consumer(i);
new Thread(pro).start();
new Thread(con).start();
}
}
{
private String name="Aaron";
private String content="Ghost";
private boolean flag=false; //设置标志位
public void setname(String name)
{
this.name=name;
}
public String getname()
{
return this.name;
}
public void setcontent(String content)
{
this.content=content;
}
public String getcontent()
{
return this.content;
}
public synchronized void set(String name,String content)
{
if(!flag)
{
try
{
super.wait();//等待消费者取出
}catch(InterruptedException e)
{
e.printStackTrace();
}
}
this.setname(name);
try{
Thread.sleep(300);
}catch(InterruptedException e)
{
e.printStackTrace();
}
this.setcontent(content);
flag=false;//重置标志位,表示可以取走
super.notify();//唤醒等待线程
}
public synchronized void get()
{
if(flag)
{
try
{
super.wait();
}catch(InterruptedException e)
{
e.printStackTrace();
}
}
try{
Thread.sleep(90);
}catch(InterruptedException e)
{
e.printStackTrace();
}
System.out.println("姓名:"+this.getname()+"内容:"+this.getcontent());
flag=true; //修改标志位,表示可以恢复生产
super.notify(); //唤醒等待线程
}
}
class producer implements Runnable
{
private Info info=null;
public producer(Info info)
{
this.info=info;
}
public void run() //run方法的覆写
{
boolean flag=false;
for(int i=0;i<20;i++)
{
if(flag)
{
this.info.set("Aaron", "Ghost");
flag=false;
}
else
{
this.info.set("husiyun", "goal");
flag=true;
}
}
}
}
class consumer implements Runnable
{
private Info info=null;
public consumer(Info info)
{
this.info=info;
}
public void run() //run方法的覆写
{
for(int i=0;i<20;i++)
{
try
{
Thread.sleep(100);
}catch(InterruptedException e)
{
e.printStackTrace();
}
this.info.get();
}
}
}
public class test70 {
public static void main(String args[])
{
Info i=new Info(); //先实例化
producer pro=new producer(i);
consumer con=new consumer(i);
new Thread(pro).start();
new Thread(con).start();
}
}
相关文章推荐
- Java 多线程应用小结(一)
- Java【多线程知识总结(7)】多线程同步问题-关于synchronized代码块和synchronized方法的应用
- java多线程并发库高级应用 之 多个线程之间共享数据的方式探讨
- Java多线程与并发库高级应用 学习笔记 16-22课 +面试题
- java 多线程并行求和 之 join() 方法应用
- 应用java多线程实现服务器端与多客户端之间的通信
- Java Volatile关键字 以及long,double在多线程中的应用
- java多线程 —— 两种实际应用场景模拟
- 多线程中,java锁的应用
- JAVA多线程并发库高级应用 (一)
- JAVA多线程并发库高级应用 (五)
- Java多线程与并发库高级应用-传统线程机制回顾
- Java多线程与并发库高级应用-传统线程同步通信技术
- java多线程应用场景
- 【Java多线程与并发库】8.java5线程并发库之线程池的应用
- 【多线程】java.util.concurrent.Exchanger应用范例与原理浅析
- Java多线程之Synchronized应用在static和非static方法上的区别
- Java多线程之Lock的应用
- 转:java多线程小结,及解决应用挂死的问题
- JAVA---多线程之Callable与Future,FutureTask,及其简单应用