一个简单的多线程问题,欢迎指教! synchronized, wait(), notify() 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 java早期支持线程的suspend,resume和stop方法但是现在已经废除了,所以需要自己写扩展类class CustomThread extends Thread { volatile boolean goFlag = true; CustomThread(String name) { super(name); start(); } public void run() { try { for(int loop_index = 0; loop_index <= 5; loop_index++) { System.out.println(Thread.currentThread().getName() + " here..."); Thread.sleep(500); synchronized(this) { while(!goFlag) { wait(); } } } } catch (InterruptedException e) {} } public void newSuspend() { goFlag = false; } synchronized public void newResume() { goFlag = true; notify(); }}class suspend { public static void main(String args[]) { CustomThread thread1 = new CustomThread("one"); CustomThread thread2 = new CustomThread("two"); try { Thread.sleep(1000); System.out.println("Suspending thread one..."); thread1.newSuspend(); Thread.sleep(1000); System.out.println("Resuming thread one..."); thread1.newResume(); } catch (InterruptedException e) {} try { thread1.join(); thread2.join(); } catch (InterruptedException e) {} }}把程序贴给你,希望对你有帮助 监听器结合java定时器做的一个任务调度,为什么不执行? 如何控制有60%的几率出现一个数? Socket数据的显示,在线等!!! 关于list添加多条数据的问题 我的程序编译有问题,请帮忙改正! 关于事件和监听器 关于new 请问<<java核心技术>>第6版这本书值得买吗? 有关rsa的privateKey问题,已经调了2天了 学习J2EE需要装什么软件? 高手,如何在JBuilder6下,创建Hello,Java!工程,有*.jpx,*.java等等文件,麻烦具体点,菜鸟希望高手能伸出援助之手! 入门问题--Java能做什么?
但是现在已经废除了,所以需要自己写扩展类class CustomThread extends Thread
{
volatile boolean goFlag = true;
CustomThread(String name)
{
super(name);
start();
} public void run()
{
try {
for(int loop_index = 0; loop_index <= 5; loop_index++) {
System.out.println(Thread.currentThread().getName() + " here...");
Thread.sleep(500);
synchronized(this) {
while(!goFlag) {
wait();
}
}
}
} catch (InterruptedException e) {}
} public void newSuspend()
{
goFlag = false;
} synchronized public void newResume()
{
goFlag = true;
notify();
}
}class suspend
{
public static void main(String args[])
{
CustomThread thread1 = new CustomThread("one");
CustomThread thread2 = new CustomThread("two"); try {
Thread.sleep(1000);
System.out.println("Suspending thread one...");
thread1.newSuspend();
Thread.sleep(1000);
System.out.println("Resuming thread one...");
thread1.newResume();
} catch (InterruptedException e) {} try {
thread1.join();
thread2.join();
} catch (InterruptedException e) {}
}
}
把程序贴给你,希望对你有帮助