JAVA主线程与子线程操作
Java代码
-
public
class ImportThread extends Thread { -
private
static List runningThreads = new ArrayList(); -
public
ImportThread() { -
}
-
@Override
-
public
void run() { -
regist(this);//线程开始时注册
-
System.out.println(Thread.currentThread().getName()
+ "开始...");//打印开始标记 -
//做一些事情...
... -
unRegist(this);//线程结束时取消注册
-
System.out.println(Thread.currentThread().getName()
+ "结束.");//打印结束标记 -
}
-
public
void regist(Thread t){ -
synchronized(runningThreads){ -
runningThreads.add(t); -
} -
}
-
public
void unRegist(Thread t){ -
synchronized(runningThreads){ -
runningThreads.remove(t); -
} -
}
-
public
static boolean hasThreadRunning() { -
return
(runningThreads.size() > 0);//通过判断runningThreads是否为空就能知道是否还有线程未执行完 -
}
-
}
Java代码
-
long
tStart = System.currentTimeMillis(); -
System.out.println(Thread.currentThread().getName()
+ "开始");//打印开始标记 -
for
(int ii = 0; ii < threadNum; ii++) {//开threadNum个线程 -
Thread
t = new ImportThread(); -
t.start();
-
}
-
while(true){//等待所有子线程执行完
-
if(!ImportThread.hasThreadRunning()){
-
break;
-
}
-
Thread.sleep(500);
-
}
-
System.out.println(Thread.currentThread().getName()
+ "结束.");//打印结束标记 -
long
tEnd = System.currentTimeMillis(); -
System.out.println("总共用时:"+
(tEnd - tStart) + "millions");
Java代码
-
public
class MyCountDown { -
private
int count; -
public
MyCountDown(int count){ -
this.count
= count; -
}
-
public
synchronized void countDown(){ -
count--;
-
}
-
public
synchronized boolean hasNext(){ -
return
(count > 0); -
}
-
public
int getCount() { -
return
count; -
}
-
public
void setCount(int count) { -
this.count
= count; -
}
-
}
Java代码
-
public
class ImportThread extends Thread { -
private
MyCountDown c; -
public
ImportThread(MyCountDown c) { -
this.c
= c; -
}
-
@Override
-
public
void run() { -
System.out.println(Thread.currentThread().getName()
+ "开始...");//打印开始标记 -
//Do
something -
c.countDown();//计时器减1
-
System.out.println(Thread.currentThread().getName()
+ "结束. 还有" + c.getCount() + " 个线程" );//打印结束标记 -
}
-
}
Java代码
-
System.out.println(Thread.currentThread().getName()
+ "开始");//打印开始标记 -
MyCountDown
c = new MyCountDown(threadNum);//初始化countDown -
for
(int ii = 0; ii < threadNum; ii++) {//开threadNum个线程 -
Thread
t = new ImportThread(c); -
t.start();
-
}
-
while(true){//等待所有子线程执行完
-
if(!c.hasNext())
break; -
}
-
System.out.println(Thread.currentThread().getName()
+ "结束.");//打印结束标记
Java代码
-
public
class ImportThread extends Thread { -
private
CountDownLatch threadsSignal; -
public
ImportThread(CountDownLatch threadsSignal) { -
this.threadsSignal
= threadsSignal; -
}
-
@Override
-
public
void run() { -
System.out.println(Thread.currentThread().getName()
+ "开始..."); -
//Do
somethings -
threadsSignal.countDown();//线程结束时计数器减1
-
System.out.println(Thread.currentThread().getName()
+ "结束. 还有" + threadsSignal.getCount() + " 个线程" ); -
}
-
}
Java代码
-
CountDownLatch
threadSignal = new CountDownLatch(threadNum);//初始化countDown -
for
(int ii = 0; ii < threadNum; ii++) {//开threadNum个线程 -
final
Iterator itt = it.get(ii); -
Thread
t = new ImportThread(itt,sql,threadSignal); -
t.start();
-
}
-
threadSignal.await();//等待所有子线程执行完
-
System.out.println(Thread.currentThread().getName()
+ "结束.");//打印结束标记