David Toy's blogtoydavid.blog.chinaunix.net
cj_gameboy
全部博文(284)
joomla(1)
git(8)
TWave(1)
项目(1)
介绍(6)
错误处理(1)
练习(25)
utf-8(1)
GTK(17)
X-Windows(1)
linux驱动程序(1)
yacc与lex(1)
压缩(1)
信号处理(1)
函数(2)
shuffle压缩算法(1)
旋转门压缩(1)
哈夫曼压缩(4)
小波压缩(2)
压缩(0)
静态库与动态库(4)
调试技术(8)
杂谈(4)
oracle(3)
socket(7)
文件系统相关(1)
多线程(8)
多进程(1)
网络相关(0)
数据库操作(0)
ipc相关(7)
mysql(7)
ftp(2)
算法(1)
基本应用(1)
编程练习(2)
进程列表(1)
direct(1)
MFC(1)
visual studio(1)
DLL技术(1)
win核心技术(2)
UDP(1)
TCPIP(1)
有名管道(1)
邮槽技术(2)
win-IPC(0)
string分解(1)
STL相关(1)
fstream(4)
boost相关(6)
实时数据库(1)
MSserver(2)
oracle技术(20)
2012年(18)
2011年(33)
2010年(83)
2009年(147)
2008年(3)
maoyg082
小雅贝贝
diaoopad
Moonecho
hjshajsh
浪花小雨
cynthia
学无止境
fxtxkktv
麻烦的c
安然之风
12044626
分类: Java
2010-07-26 19:30:56
class NewThread extends Thread{ boolean suspendFlag; NewThread(String threadname, ThreadGroup tgOb){ super(tgOb, threadname); System.out.println("New thread: "+this); suspendFlag = false; start(); } public void run(){ try{ for(int i=5;i>0;i--){ System.out.println(getName()+": "+i); Thread.sleep(1000); synchronized(this){ while(suspendFlag){ wait(); } } } }catch(Exception e){ System.out.println("Exception in "+getName()); } System.out.println(getName()+"exiting."); } void mysuspend(){ suspendFlag = true; } synchronized void myresume(){ suspendFlag = false; notify(); }}public class test { public static void main(String[] args) { // TODO Auto-generated method stub ThreadGroup groupA = new ThreadGroup("Group A"); ThreadGroup groupB = new ThreadGroup("Group B"); NewThread ob1 = new NewThread("one", groupA); NewThread ob2 = new NewThread("Tow", groupA); NewThread ob3 = new NewThread("three", groupB); NewThread ob4 = new NewThread("Four", groupB); System.out.println("Suspending Group A"); Thread tga[] = new Thread[groupA.activeCount()]; groupA.enumerate(tga); for(int i=0;i<tga.length;i++){ ((NewThread)tga[i]).mysuspend(); } try { Thread.sleep(4000); }catch(InterruptedException e){ System.out.println("Main thread interrupted."); } System.out.println("Resuming Group A"); for(int i=0;i<tga.length;i++){ ((NewThread)tga[i]).myresume(); } try { System.out.println("Waiting for threads to finish."); ob1.join(); ob2.join(); ob3.join(); ob4.join(); }catch (Exception e){ System.out.println("Exception in Main thread"); } System.out.println("Main thread exiting."); }}
上一篇:java-定时器
下一篇:java文件系统
登录 注册