1.進(jìn)程與線程
每個(gè)進(jìn)程都獨(dú)享一塊內(nèi)存空間,一個(gè)應(yīng)用程序可以同時(shí)啟動(dòng)多個(gè)進(jìn)程。比如IE瀏覽器,打開一個(gè)Ie瀏覽器就相當(dāng)于啟動(dòng)了一個(gè)進(jìn)程。
線程指進(jìn)程中的一個(gè)執(zhí)行流程,一個(gè)進(jìn)程可以包含多個(gè)線程。
每個(gè)進(jìn)程都需要操作系統(tǒng)為其分配獨(dú)立的內(nèi)存空間,而同一個(gè)進(jìn)程中的多個(gè)線程共享這塊空間,即共享內(nèi)存等資源。
每次調(diào)用java.exe的時(shí)候,操作系統(tǒng)都會(huì)啟動(dòng)一個(gè)Java虛擬機(jī)進(jìn)程,當(dāng)啟動(dòng)Java虛擬機(jī)進(jìn)程時(shí)候,Java虛擬機(jī)都會(huì)創(chuàng)建一個(gè)主線程,該線程會(huì)從程序入口main方法開始執(zhí)行。
Java虛擬機(jī)每啟動(dòng)一個(gè)線程,就給會(huì)給該線程分配一個(gè)線程方法棧,用來存放相關(guān)信息(比如局部變量等),線程就在這個(gè)棧上運(yùn)行。所以Java對象中的局部變量都是線程安全的,但實(shí)例變量及類變量由于不是保存在棧中,所以不是線程安全的。
進(jìn)程有三種狀態(tài):就緒、執(zhí)行、阻塞。
2.線程創(chuàng)建方式
Runnable方式:(此種方式靈活,推薦使用)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
public class Thread02 implements Runnable { public static void main(String[] args) { Runnable r = new <strong>Thread02</strong>(); Thread t1 = new Thread(<strong>r</strong>, "t1" ); /** * Thread源碼 * public Thread(Runnable target, String name) { init(null, target, name, 0); } */ Thread t2 = new Thread(r, "t2" ); t1.start(); // 啟動(dòng)線程t1,處于就緒狀態(tài),等待cpu t2.start(); // 啟動(dòng)線程t2,處于就緒狀態(tài),等待cpu t1.run(); // 主線程main調(diào)用對象t1的run方法 } public void run() { System.out.println( "thread's name is " + Thread.currentThread().getName()); } } |
運(yùn)行結(jié)果可能是:
1
2
3
|
thread's name is t1 thread's name is main thread's name is t2 |
Thead方式
1
2
3
4
5
6
7
8
9
10
11
12
|
public class Thread03 extends Thread { public static void main(String[] args) { Thread03 t1 = new <strong>Thread03</strong>(); //不注意的情況下寫成了Thread t1=new Thread() 注:Thread03此時(shí)就是一個(gè)線程了 t1.start(); } public void run() { System.out.println( "thread's name is " + Thread.currentThread().getName()); } } |
運(yùn)行結(jié)果:thread's name is Thread-0
注意:每次程序運(yùn)行時(shí)除了自定義的線程外還有一個(gè)main線程。
綜合:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
public class Thread01 { public static void main(String[] args) { Thread thread= new Thread(); thread.start(); //真正起作用 的是run() /**而Thread中的run * public void run() { if (target != null) { target.run(); } } 所以自己創(chuàng)建的線程要重寫run方法,把要執(zhí)行的內(nèi)容放到run()中,所以要實(shí)現(xiàn)接口或繼承進(jìn)而產(chǎn)生子類 */ //創(chuàng)建線程的方式1 thread子類方式(繼承) Thread thread1= new Thread(){ public void run() { while ( true ){ try { Thread.sleep( 500 ); //休息500毫秒 } catch (InterruptedException e) { e.printStackTrace(); } //Thread.currentThread()得到當(dāng)前線程 System.out.println( "線程1的名字是 " +Thread.currentThread().getName()); } } }; // thread1.start(); //不寫 線程無法啟動(dòng) //創(chuàng)建線程的方式2 runnable方式(實(shí)現(xiàn)) 推薦使用 Thread thread2= new Thread( new Runnable(){ public void run() { while ( true ){ try { Thread.sleep( 300 ); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println( "thread2'name is " +Thread.currentThread().getName()); } }}); // thread2.start(); //執(zhí)行的是thread new Thread( new Runnable(){ public void run() { System.out.println( "runnable " +Thread.currentThread().getName()); }}){ public void run() { //子類中的run方法覆蓋父類中的run方法,這樣就不會(huì)執(zhí)行runnable System.out.println( "thread " +Thread.currentThread().getName()); } }.start(); } /*** * 在單個(gè)cpu中執(zhí)行多線程很有可能降低執(zhí)行效率而不是提高 一個(gè)人在不同地方做同一件事情 */ } |
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
原文鏈接:http://www.cnblogs.com/wangchenyang/archive/2011/08/27/2155823.html