Java多线程实践
来源:互联网 发布:手机关机定位软件 编辑:程序博客网 时间:2024/05/16 11:16
Sun提供的:
/**//*
* Producer.java
*
* Created on 2008年2月24日, 下午2:11
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package threadPC;
/** *//**
*
* @author rulinma
*/
public class Producer extends Thread
...{
private CubbyHole cubbyhole;
private int number;
private Producer()
...{
}
public Producer(CubbyHole c, int number)
...{
cubbyhole = c;
this.number = number;
}
public void run()
...{
for (int i = 0; i < 10; i++)
...{
System.out.println("Producer #" + this.number + " put: " + i);
cubbyhole.put(i);
* Producer.java
*
* Created on 2008年2月24日, 下午2:11
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package threadPC;
/** *//**
*
* @author rulinma
*/
public class Producer extends Thread
...{
private CubbyHole cubbyhole;
private int number;
private Producer()
...{
}
public Producer(CubbyHole c, int number)
...{
cubbyhole = c;
this.number = number;
}
public void run()
...{
for (int i = 0; i < 10; i++)
...{
System.out.println("Producer #" + this.number + " put: " + i);
cubbyhole.put(i);
//System.out.println("Producer #" + this.number + " put: " + i);
try
...{
sleep((int)(Math.random() * 100));
}
catch (InterruptedException e)
...{
}
}
}
}
try
...{
sleep((int)(Math.random() * 100));
}
catch (InterruptedException e)
...{
}
}
}
}
/**//*
* Consumer.java
*
* Created on 2008年2月24日, 下午2:16
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package threadPC;
/** *//**
*
* @author rulinma
*/
public class Consumer extends Thread ...{
private CubbyHole cubbyhole;
private int number;
/** *//** Creates a new instance of Consumer */
public Consumer() ...{
}
public Consumer(CubbyHole c, int number)
...{
cubbyhole = c;
this.number = number;
}
public void run()
...{
int value = 0;
for (int i = 0; i < 10; i++)
...{
value = cubbyhole.get();
System.out.println("Consumer #" + this.number + " got: " + value);
}
}
}
* Consumer.java
*
* Created on 2008年2月24日, 下午2:16
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package threadPC;
/** *//**
*
* @author rulinma
*/
public class Consumer extends Thread ...{
private CubbyHole cubbyhole;
private int number;
/** *//** Creates a new instance of Consumer */
public Consumer() ...{
}
public Consumer(CubbyHole c, int number)
...{
cubbyhole = c;
this.number = number;
}
public void run()
...{
int value = 0;
for (int i = 0; i < 10; i++)
...{
value = cubbyhole.get();
System.out.println("Consumer #" + this.number + " got: " + value);
}
}
}
/**//*
* CubbyHole.java
*
* Created on 2008年2月24日, 下午2:05
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package threadPC;
/** *//**
*
* @author rulinma
*/
public class CubbyHole
...{
private int contents;
private boolean available = false;
public CubbyHole()
...{
}
/**//*
* synchronized同步提供单一线程访问
*/
public synchronized int get()
...{
//System.out.println("aviaialbe " + available);
while (available == false)
...{
try
...{
//System.out.println("get waiting...");
wait();
//available = false;
}
catch (InterruptedException e)
...{
}
}
notifyAll();
available = false;
//System.out.println("contents:"+contents);
return contents;
}
public synchronized void put(int value)
...{
//System.out.println("aviaialbe " + available);
while (available == true)
...{
try
...{
//System.out.println("put waiting...");
wait();
//available = true;
}
catch (InterruptedException e)
...{
}
}
contents = value;
notifyAll();
available = true;
//System.out.println("contents:"+contents);
}
}
* CubbyHole.java
*
* Created on 2008年2月24日, 下午2:05
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package threadPC;
/** *//**
*
* @author rulinma
*/
public class CubbyHole
...{
private int contents;
private boolean available = false;
public CubbyHole()
...{
}
/**//*
* synchronized同步提供单一线程访问
*/
public synchronized int get()
...{
//System.out.println("aviaialbe " + available);
while (available == false)
...{
try
...{
//System.out.println("get waiting...");
wait();
//available = false;
}
catch (InterruptedException e)
...{
}
}
notifyAll();
available = false;
//System.out.println("contents:"+contents);
return contents;
}
public synchronized void put(int value)
...{
//System.out.println("aviaialbe " + available);
while (available == true)
...{
try
...{
//System.out.println("put waiting...");
wait();
//available = true;
}
catch (InterruptedException e)
...{
}
}
contents = value;
notifyAll();
available = true;
//System.out.println("contents:"+contents);
}
}
/**//*
* ProducerConsumerTest.java
*
* Created on 2008年2月24日, 下午2:18
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package threadPC;
/** *//**
*
* @author rulinma
*/
public class ProducerConsumerTest ...{
public ProducerConsumerTest() ...{
}
public static void main(String[] args)
...{
CubbyHole c = new CubbyHole();
Producer p1 = new Producer(c, 1);
Consumer c1 = new Consumer(c, 1);
p1.start();
c1.start();
}
}
* ProducerConsumerTest.java
*
* Created on 2008年2月24日, 下午2:18
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package threadPC;
/** *//**
*
* @author rulinma
*/
public class ProducerConsumerTest ...{
public ProducerConsumerTest() ...{
}
public static void main(String[] args)
...{
CubbyHole c = new CubbyHole();
Producer p1 = new Producer(c, 1);
Consumer c1 = new Consumer(c, 1);
p1.start();
c1.start();
}
}
运行结果:
init:
deps-jar:
compile-single:
run-single:
Producer #1 put: 0
Consumer #1 got: 0
Producer #1 put: 1
Consumer #1 got: 1
Producer #1 put: 2
Consumer #1 got: 2
Producer #1 put: 3
Consumer #1 got: 3
Producer #1 put: 4
Consumer #1 got: 4
Producer #1 put: 5
Consumer #1 got: 5
Producer #1 put: 6
Consumer #1 got: 6
Producer #1 put: 7
Consumer #1 got: 7
Producer #1 put: 8
Consumer #1 got: 8
Producer #1 put: 9
Consumer #1 got: 9
生成成功(总时间:1 秒)
运行过程如果使用上述代码中的红色代码而不是蓝色的 运行结果会有所不同。
- Java多线程实践
- java 多线程实践
- JAVA基础--多线程实践
- JAVA基础--多线程实践
- 用java多线程断点续传实践
- 用java多线程断点续传实践
- 用java多线程断点续传实践
- java多线程实践和总结
- Java多线程实践—篇外篇
- 用java多线程断点续传实践
- Java实例:利用java多线程断点续传实践
- java初学者实践教程25-多线程
- Java5 多线程实践 -- java.util.concurrent
- Java 5.0 多线程编程实践(1)
- Java 5.0 多线程编程实践(2)
- java多线程(2):并发编程实践
- Java ExecutorService 多线程实践(一)
- Java多线程实践之基础篇
- N皇后问题
- 关于人生目标的思考
- 汉诺塔问题, 图形分页显示每一步.
- C语言字符串操作函数
- 迷宫问题
- Java多线程实践
- .net用url重写URLReWriter实现任意二级域名
- Linux ARP缓存表
- 常用的数据选择
- 转义字符的完整诠释
- 液晶面板类型详解
- 关于数据库的权限设置深入剖析
- Zoundry 博客编辑器用户指南
- Basic语言写的俄罗斯方块