本文参考Think in java,好久没接触线程了,温习一下。
基本的线程可以继承Runnable
,这是一个倒计时的小线程,倒数10,9….3,2,1
import java.text.SimpleDateFormat;
import java.util.Date;
import Entry.Main;
public class LiftOff implements Runnable{
public LiftOff() {
super();
}
//计数值
private int countDown=10;
//ID生成
private static int taskCount=0;
//线程ID
private final int ThreadID=taskCount++;
@Override
public void run() {
while(countDown>0) {
Main.logIndex++;
System.out.println(Main.logIndex+" "+"[Info]["+System.currentTimeMillis()+"]"+"----"+this.status());
countDown--;
Thread.yield();
}
}
//返回线程状态,即countDown的值
private String status() {
String status="#"+"Thread["+ThreadID+"]:";
if(countDown>0) {
status=status+""+countDown;
}
else {
status="Launch";
}
return status;
}
}
你可以通过以下几个方式来发动线程,我封装在Main方法中,以注释的表达目的:
package Entry;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import ThreadStash.LiftOff;
public class Main {
//线程不安全共享变量
public static long logIndex=0;
public static void main(String[] args) {
cacheThreadPool();
}
public static void basicTheaad() {
// TODO Auto-generated method stub
LiftOff emit=new LiftOff();
emit.run();
}
private static void postToThread() {
//最后一行语句在任何一个地方都可能打印出来
Thread t=new Thread(new LiftOff());
t.start();
System.out.println("[Info]["+new Date()+"]:"+"PrintedMaybeAnywhere");
}
private static void multiThreadExecute(int num) throws NoSuchMethodException, SecurityException {
for(int i=0;i<num;i++) {
new Thread(new LiftOff()).start();
}
System.out.println("[Info]["+new Date()+"]:"+Main.class.getMethod("multiThreadExecute",Integer.TYPE).getName()+"PrintedMaybeAnywhere");
}
private static void cacheThreadPool() {
//线程池的容量,即能够容纳线程的个数是可变的
ExecutorService service=Executors.newCachedThreadPool();
for(int i=0;i<5;i++) {
service.execute(new LiftOff());
}
//shutdown方法放置新的线程加入线程池CachedThreadPool
service.shutdown();
}
}
通过调用不同的静态函数,你可以看到一些多线程运行的效果,文中打印方式时间精度为毫秒( )
1 [Info][1526142033820]----#Thread[0]:10
2 [Info][1526142033821]----#Thread[1]:10
3 [Info][1526142033821]----#Thread[2]:10
4 [Info][1526142033821]----#Thread[3]:10
5 [Info][1526142033822]----#Thread[4]:10
6 [Info][1526142033822]----#Thread[0]:9
7 [Info][1526142033822]----#Thread[1]:9
8 [Info][1526142033822]----#Thread[2]:9
9 [Info][1526142033823]----#Thread[3]:9
13 [Info][1526142033823]----#Thread[2]:8
12 [Info][1526142033823]----#Thread[1]:8
11 [Info][1526142033823]----#Thread[4]:9
10 [Info][1526142033823]----#Thread[0]:8
17 [Info][1526142033823]----#Thread[4]:8
16 [Info][1526142033823]----#Thread[1]:7
15 [Info][1526142033823]----#Thread[2]:7
14 [Info][1526142033823]----#Thread[3]:8
18 [Info][1526142033823]----#Thread[0]:7
21 [Info][1526142033824]----#Thread[2]:6
19 [Info][1526142033823]----#Thread[4]:7
20 [Info][1526142033823]----#Thread[1]:6
22 [Info][1526142033824]----#Thread[3]:7
23 [Info][1526142033824]----#Thread[2]:5
24 [Info][1526142033824]----#Thread[4]:6
25 [Info][1526142033824]----#Thread[0]:6
26 [Info][1526142033824]----#Thread[1]:5
27 [Info][1526142033824]----#Thread[3]:6
28 [Info][1526142033824]----#Thread[2]:4
29 [Info][1526142033824]----#Thread[0]:5
30 [Info][1526142033824]----#Thread[4]:5
31 [Info][1526142033824]----#Thread[1]:4
32 [Info][1526142033824]----#Thread[3]:5
33 [Info][1526142033824]----#Thread[2]:3
34 [Info][1526142033825]----#Thread[0]:4
36 [Info][1526142033825]----#Thread[1]:3
35 [Info][1526142033825]----#Thread[4]:4
37 [Info][1526142033825]----#Thread[2]:2
38 [Info][1526142033825]----#Thread[3]:4
41 [Info][1526142033825]----#Thread[0]:3
39 [Info][1526142033825]----#Thread[1]:2
44 [Info][1526142033825]----#Thread[0]:2
43 [Info][1526142033825]----#Thread[3]:3
42 [Info][1526142033825]----#Thread[2]:1
40 [Info][1526142033825]----#Thread[4]:3
45 [Info][1526142033825]----#Thread[0]:1
47 [Info][1526142033825]----#Thread[3]:2
46 [Info][1526142033825]----#Thread[1]:1
48 [Info][1526142033825]----#Thread[4]:2
49 [Info][1526142033825]----#Thread[3]:1
50 [Info][1526142033826]----#Thread[4]:1