多线程举例! 同志们,能举些多线程的例子给哥看看吗? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://topic.csdn.net/u/20100627/18/dfd80ebc-63a1-4132-ab07-25a45e1ef620.html?88886 生产者消费者===现成同步举例;package lession.lession6;import java.util.*;//==========================模拟销售馒头的线程同步============================/*class Ham{ public static Object box=new Object(); public static int material=6; //有材料6个可以做6个 馒头 public static int sales=0; public static int production=3;}class Hammaker extends Thread{ private void Make(){ synchronized(Ham.box){ Ham.production++; int num=Ham.production-Ham.sales; System.out.println("<厨师>:这里有馒头"+num+"个,拿去卖"); Ham.box.notify(); // 唤醒服务员线程 如果把这里注释的话,会让服务员线程一直等待了, } } public void run(){ while(Ham.production<=Ham.material){ try{ sleep(3000); }catch(InterruptedException ie){} Make(); //没隔1秒做一个馒头 } }}class HAssistant extends Thread{ private void Sell(){ synchronized(Ham.box){ if(Ham.production==Ham.sales){ System.out.println("<服务员>:请稍后,馒头马上就来"); try{ Ham.box.wait(); }catch(InterruptedException ie){} } int num=Ham.production-Ham.sales; Ham.sales++; System.out.println("<服务员>:顾客门,馒头来了哦,一共有"+num+"个"); } } public void run(){ while(Ham.sales<=Ham.material){ System.out.println("=============<顾客门来买馒头了>==========="); Sell(); try{ sleep(1000);//每隔一秒就卖一个馒头 }catch(InterruptedException ie){} } }}public class thread1{ public static void main(String []args){ Hammaker maker=new Hammaker(); HAssistant assistant =new HAssistant(); maker.start(); assistant.start(); try{ maker.join(); //等待该线执行完毕 assistant.join(); } catch(InterruptedException ie){} System.out.println(); System.out.println(Ham.sales+"\n"+Ham.production); }}/* http://download.csdn.net/source/741733 也可以下载个。 JAVA 如何重画直线 求一条正则表达式 关于加载水晶报表!请各位看看! 请问处理 一个日历小程序空值输出问题 请问怎么用BufferedOutputStream中的write方法往XML文件中写入UTF-8编码字符串?(编码问题) 代码中嵌入<APPLET>标记的问题 xue_sharp(你是自愿被我泡的) 近来领分!!!! 请问各位高手Mon Sep 02 02:00:00 CST 2002 怎样转换成2002-9-2 2:00:00啊 append()方法在哪些类里有啊?分别有什么作用?老是看到这个方法 跪求一篇计算机网络课程设计 JAVA 编程的问题
import java.util.*;
//==========================模拟销售馒头的线程同步============================
/*
class Ham{
public static Object box=new Object();
public static int material=6; //有材料6个可以做6个 馒头
public static int sales=0;
public static int production=3;
}
class Hammaker extends Thread{
private void Make(){
synchronized(Ham.box){
Ham.production++;
int num=Ham.production-Ham.sales;
System.out.println("<厨师>:这里有馒头"+num+"个,拿去卖");
Ham.box.notify(); // 唤醒服务员线程 如果把这里注释的话,会让服务员线程一直等待了,
}
}
public void run(){
while(Ham.production<=Ham.material){
try{
sleep(3000);
}catch(InterruptedException ie){}
Make(); //没隔1秒做一个馒头
}
}
}
class HAssistant extends Thread{
private void Sell(){
synchronized(Ham.box){
if(Ham.production==Ham.sales){
System.out.println("<服务员>:请稍后,馒头马上就来");
try{
Ham.box.wait();
}catch(InterruptedException ie){}
}
int num=Ham.production-Ham.sales;
Ham.sales++;
System.out.println("<服务员>:顾客门,馒头来了哦,一共有"+num+"个");
}
}
public void run(){
while(Ham.sales<=Ham.material){
System.out.println("=============<顾客门来买馒头了>===========");
Sell();
try{
sleep(1000);//每隔一秒就卖一个馒头
}catch(InterruptedException ie){}
}
}
}
public class thread1{
public static void main(String []args){
Hammaker maker=new Hammaker();
HAssistant assistant =new HAssistant();
maker.start();
assistant.start();
try{
maker.join(); //等待该线执行完毕
assistant.join();
}
catch(InterruptedException ie){}
System.out.println();
System.out.println(Ham.sales+"\n"+Ham.production);
}
}
/*