请教一个多线程的问题 多线程的问题,一直没有太弄明白。现在有一个问题请教一下。或者谁有好的多线程相关文章地址给我参考一下。题目如下:厨师做饭,服务员上菜,服务员上菜要等厨师做好才能上菜,上完菜继续等。用多线程实现。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 生产者消费者问题BlockingQueue的take和put方法就是你要的 你可以看看,java.util.concurrent包下的类,一个简单的,子,主线程列子, 下面一个是网上看到的,CountDownLatch控制线程的例子public class TheardTest { /** * @param args */ public static void main(String[] args) { final Condition c=new Condition(); /*new Runnable() { @Override public synchronized void run() { for(int i=0;i<5;i++){ c.sub(); System.out.println("*****************************"+Thread.currentThread().getName()+" --执行 :"+(i+1)+" 轮"); } } }.run();*/ new Thread("子线程"){ public void run(){ for(int i=0;i<5;i++){ c.sub(); System.out.println("*****************************"+Thread.currentThread().getName()+" --执行 :"+(i+1)+" 轮"); } } }.start(); for(int i=0;i<5;i++){ c.main(); System.out.println("*****************************"+Thread.currentThread().getName()+" --执行 :"+(i+1)+" 轮"); } } }class Condition{ boolean bool=true; public synchronized void main(){ if(bool){ try { this.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } for(int i=1;i<=10;i++){ try { Thread.sleep(200); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+" --执行 :"+i+" 次"); } bool=true; this.notify(); } public synchronized void sub(){ if(!bool){ try { this.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } for(int i=1;i<=50;i++){ try { Thread.sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+" --执行 :"+i+" 次"); } bool=false; this.notify(); } }import java.util.Random;import java.util.concurrent.CountDownLatch;import java.util.concurrent.TimeUnit;/** * @author zuxiang * */public class Worker implements Runnable{ private CountDownLatch downLatch; private String name; public Worker(CountDownLatch downLatch, String name){ this.downLatch = downLatch; this.name = name; } public void run() { this.doWork(); try{ TimeUnit.SECONDS.sleep(new Random().nextInt(10)); }catch(InterruptedException ie){ } System.out.println(this.name + "活干完了!"); this.downLatch.countDown(); } private void doWork(){ System.out.println(this.name + "正在干活!"); } } /** * @author zuxiang * Java的concurrent包里面的CountDownLatch其实可以把它看作一个计数器,只不过这个计数器的操作是原子操作 * ,同时只能有一个线程去操作这个计数器,也就是同时只能有一个线程去减这个计数器里面的值。 * 你可以向CountDownLatch对象设置一个初始的数字作为计数值 * ,任何调用这个对象上的await()方法都会阻塞,直到这个计数器的计数值被其他的线程减为0为止。 * CountDownLatch的一个非常典型的应用场景是:有一个任务想要往下执行,但必须要等到其他的任务执行完毕后才可以继续往下执行。 * 假如我们这个想要继续往下执行的任务调用一个CountDownLatch对象的await * ()方法,其他的任务执行完自己的任务后调用同一个CountDownLatch对象上的countDown * ()方法,这个调用await()方法的任务将一直阻塞等待,直到这个CountDownLatch对象的计数值减到0为止。 * 举个例子,有三个工人在为老板干活 * ,这个老板有一个习惯,就是当三个工人把一天的活都干完了的时候,他就来检查所有工人所干的活。记住这个条件:三个工人先全部干完活 * ,老板才检查。所以在这里用Java代码设计两个类,Worker代表工人,Boss代表老板,具体的代码实现如下: * */public class CountDownLatchTest { public static void main(String[] args) { ExecutorService executor = Executors.newCachedThreadPool(); CountDownLatch latch = new CountDownLatch(3); Worker w1 = new Worker(latch,"张三"); Worker w2 = new Worker(latch,"李四"); Worker w3 = new Worker(latch,"王二"); Boss boss = new Boss(latch); executor.execute(w3); executor.execute(w2); executor.execute(w1); executor.execute(boss); executor.shutdown(); }} 这个吧,相关文章http://www.cnblogs.com/pingyuyue/archive/2012/02/10/2345122.html 关于jsp调用ocx控件的问题 请问做一个类似京都规模的商城估计要多久啊? <c:forEach 输出Set集合的问题 Hibernate删除list问题 struts问题 关于验证 刚转到Java请各位指点. 请教一下tomcat5下配置struts问题!!! 有没有Java学习群的QQ号啊? 用JSP调用EJB时为什么总是提示本地和远程接口找不到对象?请问应如何设置Weblogic5.1? activemq5.4.2在虚拟机LINUX环境下启动不正常 日期查询
一个简单的,子,主线程列子, 下面一个是网上看到的,CountDownLatch控制线程的例子public class TheardTest { /**
* @param args
*/
public static void main(String[] args) {
final Condition c=new Condition();
/*new Runnable() {
@Override
public synchronized void run() {
for(int i=0;i<5;i++){
c.sub();
System.out.println("*****************************"+Thread.currentThread().getName()+" --执行 :"+(i+1)+" 轮");
}
}
}.run();*/
new Thread("子线程"){
public void run(){
for(int i=0;i<5;i++){
c.sub();
System.out.println("*****************************"+Thread.currentThread().getName()+" --执行 :"+(i+1)+" 轮");
}
}
}.start();
for(int i=0;i<5;i++){
c.main();
System.out.println("*****************************"+Thread.currentThread().getName()+" --执行 :"+(i+1)+" 轮");
}
}
}class Condition{
boolean bool=true;
public synchronized void main(){
if(bool){
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
for(int i=1;i<=10;i++){
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+" --执行 :"+i+" 次");
}
bool=true;
this.notify();
}
public synchronized void sub(){ if(!bool){
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
for(int i=1;i<=50;i++){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+" --执行 :"+i+" 次");
}
bool=false;
this.notify();
}
}import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;/**
* @author zuxiang
*
*/
public class Worker implements Runnable{
private CountDownLatch downLatch;
private String name;
public Worker(CountDownLatch downLatch, String name){
this.downLatch = downLatch;
this.name = name;
}
public void run() {
this.doWork();
try{
TimeUnit.SECONDS.sleep(new Random().nextInt(10));
}catch(InterruptedException ie){
}
System.out.println(this.name + "活干完了!");
this.downLatch.countDown();
}
private void doWork(){
System.out.println(this.name + "正在干活!");
}
} /**
* @author zuxiang
* Java的concurrent包里面的CountDownLatch其实可以把它看作一个计数器,只不过这个计数器的操作是原子操作
* ,同时只能有一个线程去操作这个计数器,也就是同时只能有一个线程去减这个计数器里面的值。
* 你可以向CountDownLatch对象设置一个初始的数字作为计数值
* ,任何调用这个对象上的await()方法都会阻塞,直到这个计数器的计数值被其他的线程减为0为止。
* CountDownLatch的一个非常典型的应用场景是:有一个任务想要往下执行,但必须要等到其他的任务执行完毕后才可以继续往下执行。
* 假如我们这个想要继续往下执行的任务调用一个CountDownLatch对象的await
* ()方法,其他的任务执行完自己的任务后调用同一个CountDownLatch对象上的countDown
* ()方法,这个调用await()方法的任务将一直阻塞等待,直到这个CountDownLatch对象的计数值减到0为止。
* 举个例子,有三个工人在为老板干活
* ,这个老板有一个习惯,就是当三个工人把一天的活都干完了的时候,他就来检查所有工人所干的活。记住这个条件:三个工人先全部干完活
* ,老板才检查。所以在这里用Java代码设计两个类,Worker代表工人,Boss代表老板,具体的代码实现如下:
*
*/
public class CountDownLatchTest { public static void main(String[] args) {
ExecutorService executor = Executors.newCachedThreadPool();
CountDownLatch latch = new CountDownLatch(3);
Worker w1 = new Worker(latch,"张三");
Worker w2 = new Worker(latch,"李四");
Worker w3 = new Worker(latch,"王二");
Boss boss = new Boss(latch);
executor.execute(w3);
executor.execute(w2);
executor.execute(w1);
executor.execute(boss);
executor.shutdown();
}
}
http://www.cnblogs.com/pingyuyue/archive/2012/02/10/2345122.html