谁有用java实现的多线程服务程序,在线等待,300分, sun网站上就有例子。多线程的例子,你可以去下啊。省300 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import java.lang.Runnable;import java.lang.Thread;/***************************************************** * *本程序实现两个线程,每个线程输出1到100的数字 *第一个线程输出1-10,停止,通知第二个线程 输出1-10 第二个线程停止 通知第一个线程 输出11-20 ... * *****************************************************///通过Runnable接口创建的线程public class DemoThread implements Runnable{ public DemoThread() { //声明两个对象实例,即创建两个线程 TestThread testthread1 = new TestThread(this,"1"); TestThread testthread2 = new TestThread(this,"2"); //调用strat()方法启动线程 testthread2.start(); testthread1.start(); } //声明主方法 public static void main(String[] args) { DemoThread demoThread1 = new DemoThread(); } //自定义线程的run()方法 public void run(){ TestThread t = (TestThread) Thread.currentThread(); try{ if (!t.getName().equalsIgnoreCase("1")) { synchronized(this) { //调用wait()方法,释放对象锁标志,进入等待状态 wait(); } } while(true){ System.out.println("@time in thread"+ t.getName()+ "="+ t.increaseTime()); if(t.getTime()%10 == 0) { synchronized(this) { System.out.println("****************************************"); //调用notify()方法通知等待队列中的第一个线程 notify(); //输出到达100后跳出循环进入等待状态 if ( t.getTime()==100 ) break; wait(); } } } }catch(Exception e){e.printStackTrace();} }}//通过Thread类的子类创建的线程class TestThread extends Thread{ private int time = 0 ; public TestThread(Runnable r,String name){ super(r,name); } //获取输出的数字 public int getTime(){ return time; } //增加数字的输出次数 public int increaseTime (){ return ++time; }} 我给你一些文章你读一下,不知是不是你想要的http://www.yesky.com/20020919/1631257.shtml Java多线程程序设计入门http://cn.tech.yahoo.com/021021/31/1abg0.html 用Java实现多线程服务器程序 http://www-900.ibm.com/developerWorks/cn/java/l-jsvr/index.shtml 使用JAVA建立稳定的多线程服务器 感谢josy(风尘浪子) 的程序 J2SE的关于布局管理器的疑惑 将一个元素插入到数组的指定位置中 求一个正则表达式 java SWT 求助!读取PDF文档内容的烦恼! 急 请教java interface 的问题 读取一行 jsdk.jar在那放在哪里啦 java test/test 问题出在哪里 。菜鸟求拯救! 网络编程如何入手! 用eclipse怎么作swt的窗体的开发亚?送高分~!
import java.lang.Thread;/*****************************************************
*
*本程序实现两个线程,每个线程输出1到100的数字
*第一个线程输出1-10,停止,通知第二个线程 输出1-10 第二个线程停止 通知第一个线程 输出11-20 ...
*
*****************************************************/
//通过Runnable接口创建的线程
public class DemoThread implements Runnable{ public DemoThread() {
//声明两个对象实例,即创建两个线程
TestThread testthread1 = new TestThread(this,"1");
TestThread testthread2 = new TestThread(this,"2"); //调用strat()方法启动线程
testthread2.start();
testthread1.start();
} //声明主方法
public static void main(String[] args) { DemoThread demoThread1 = new DemoThread(); } //自定义线程的run()方法
public void run(){ TestThread t = (TestThread) Thread.currentThread();
try{
if (!t.getName().equalsIgnoreCase("1")) {
synchronized(this) {
//调用wait()方法,释放对象锁标志,进入等待状态
wait();
}
}
while(true){ System.out.println("@time in thread"+ t.getName()+ "="+ t.increaseTime()); if(t.getTime()%10 == 0) {
synchronized(this) {
System.out.println("****************************************");
//调用notify()方法通知等待队列中的第一个线程
notify();
//输出到达100后跳出循环进入等待状态
if ( t.getTime()==100 ) break;
wait();
}
}
}
}catch(Exception e){e.printStackTrace();}
}}//通过Thread类的子类创建的线程
class TestThread extends Thread{
private int time = 0 ;
public TestThread(Runnable r,String name){
super(r,name);
}
//获取输出的数字
public int getTime(){
return time;
}
//增加数字的输出次数
public int increaseTime (){
return ++time;
}
}
http://www.yesky.com/20020919/1631257.shtml Java多线程程序设计入门
http://cn.tech.yahoo.com/021021/31/1abg0.html 用Java实现多线程服务器程序
http://www-900.ibm.com/developerWorks/cn/java/l-jsvr/index.shtml 使用JAVA建立稳定的多线程服务器