vs如何写多线程_java中的多线程的示例
在討論多線程之前,讓我們先討論線程。線程是進(jìn)程中輕量級(jí)的最小部分,可以與同一進(jìn)程的其他部分(其他線程)并發(fā)運(yùn)行。線程是獨(dú)立的,因?yàn)樗鼈兌加歇?dú)立的執(zhí)行路徑,這就是為什么如果一個(gè)線程中發(fā)生異常,它不會(huì)影響其他線程的執(zhí)行。進(jìn)程的所有線程共享公共內(nèi)存。同時(shí)執(zhí)行多個(gè)線程的過(guò)程稱(chēng)為多線程。
讓我們把討論總結(jié)成以下幾點(diǎn):
1. 多線程的主要目的是同時(shí)執(zhí)行程序的兩個(gè)或多個(gè)部分,以最大限度地利用CPU時(shí)間。多線程程序包含兩個(gè)或多個(gè)可以并發(fā)運(yùn)行的部分。程序的每個(gè)這樣的部分稱(chēng)為線程。
2. 線程是輕量級(jí)子進(jìn)程,它們共享公共內(nèi)存空間。在多線程環(huán)境中,受益于多線程的程序可以利用最大的CPU時(shí)間,使空閑時(shí)間保持在最小。
3.線程可以處于以下?tīng)顟B(tài)之一:
新-尚未啟動(dòng)的線程處于此狀態(tài)。
RUNNABLE——在Java虛擬機(jī)中執(zhí)行的線程處于這種狀態(tài)。
阻塞——等待監(jiān)視器鎖的阻塞線程處于這種狀態(tài)。
等待——正在無(wú)限期等待另一個(gè)線程執(zhí)行特定操作的線程處于這種狀態(tài)。
TIMED_WAITING—等待另一個(gè)線程執(zhí)行某個(gè)操作長(zhǎng)達(dá)指定等待時(shí)間的線程處于這種狀態(tài)。
終止-已退出的線程處于此狀態(tài)。
在給定的時(shí)間點(diǎn)上,線程只能處于一種狀態(tài)。
多任務(wù)vs多線程vs多處理vs并行處理
如果您是java新手,您可能會(huì)對(duì)這些術(shù)語(yǔ)感到困惑,因?yàn)樵谖覀冇懻摱嗑€程時(shí)它們經(jīng)常使用。讓我們簡(jiǎn)單地談一談。
多任務(wù)處理: 同時(shí)執(zhí)行多個(gè)任務(wù)的能力稱(chēng)為多任務(wù)處理。
多線程: 我們已經(jīng)討論過(guò)了。它是一個(gè)同時(shí)執(zhí)行多個(gè)線程的進(jìn)程。多線程也稱(chēng)為基于線程的多任務(wù)處理。
多處理: 它與多任務(wù)處理相同,但是在多處理中涉及多個(gè)cpu。另一方面,一個(gè)CPU參與多任務(wù)處理。
并行處理: 它是指在一個(gè)計(jì)算機(jī)系統(tǒng)中使用多個(gè)cpu。
在用Java創(chuàng)建線程
在Java中有兩種創(chuàng)建線程的方法:
1)通過(guò)擴(kuò)展Thread類(lèi)。
2)通過(guò)實(shí)現(xiàn)Runnable接口。
在開(kāi)始創(chuàng)建線程的程序(代碼)之前,讓我們先看看Thread類(lèi)的這些方法。在下面的示例中,我們很少使用這些方法。
- getName():用于獲取線程的名稱(chēng)
 - getPriority():獲取線程的優(yōu)先級(jí)
 - isAlive():確定線程是否仍在運(yùn)行
 - join():等待線程終止
 - run():線程的入口點(diǎn)
 - sleep():掛起線程一段時(shí)間
 - start():通過(guò)調(diào)用線程的run()方法來(lái)啟動(dòng)線程
 
方法1:通過(guò)擴(kuò)展線程類(lèi)創(chuàng)建線程Example 1:
class MultithreadingDemo extends Thread{ public void run(){ System.out.println("My thread is in running state."); } public static void main(String args[]){ MultithreadingDemo obj=new MultithreadingDemo(); obj.start(); } }Output:
My thread is in running state.Example 2:
class Count extends Thread{ Count() { super("my extending thread"); System.out.println("my thread created" + this); start(); } public void run() { try { for (int i=0 ;i<10;i++) { System.out.println("Printing the count " + i); Thread.sleep(1000); } } catch(InterruptedException e) { System.out.println("my thread interrupted"); } System.out.println("My thread run is over" ); }}class ExtendingExample{ public static void main(String args[]) { Count cnt = new Count(); try { while(cnt.isAlive()) { System.out.println("Main thread will be alive till the child thread is live"); Thread.sleep(1500); } } catch(InterruptedException e) { System.out.println("Main thread interrupted"); } System.out.println("Main thread's run is over" ); }}輸出:
my thread createdThread[my runnable thread,5,main]Main thread will be alive till the child thread is livePrinting the count 0Printing the count 1Main thread will be alive till the child thread is livePrinting the count 2Main thread will be alive till the child thread is livePrinting the count 3Printing the count 4Main thread will be alive till the child thread is livePrinting the count 5Main thread will be alive till the child thread is livePrinting the count 6Printing the count 7Main thread will be alive till the child thread is livePrinting the count 8Main thread will be alive till the child thread is livePrinting the count 9mythread run is overMain thread run is over方法2:通過(guò)實(shí)現(xiàn)Runnable接口創(chuàng)建線程
一個(gè)簡(jiǎn)單示例
class MultithreadingDemo implements Runnable{ public void run(){ System.out.println("My thread is in running state."); } public static void main(String args[]){ MultithreadingDemo obj=new MultithreadingDemo(); Thread tobj =new Thread(obj); tobj.start(); } }輸出:
My thread is in running state.示例程序2:
觀察這個(gè)程序的輸出,并嘗試?yán)斫膺@個(gè)程序中發(fā)生了什么。如果您已經(jīng)理解了每個(gè)線程方法的用法,那么您應(yīng)該不會(huì)遇到任何問(wèn)題,請(qǐng)理解這個(gè)示例。
class Count implements Runnable{ Thread mythread ; Count() { mythread = new Thread(this, "my runnable thread"); System.out.println("my thread created" + mythread); mythread.start(); } public void run() { try { for (int i=0 ;i<10;i++) { System.out.println("Printing the count " + i); Thread.sleep(1000); } } catch(InterruptedException e) { System.out.println("my thread interrupted"); } System.out.println("mythread run is over" ); }}class RunnableExample{ public static void main(String args[]) { Count cnt = new Count(); try { while(cnt.mythread.isAlive()) { System.out.println("Main thread will be alive till the child thread is live"); Thread.sleep(1500); } } catch(InterruptedException e) { System.out.println("Main thread interrupted"); } System.out.println("Main thread run is over" ); }}輸出:
my thread createdThread[my runnable thread,5,main]Main thread will be alive till the child thread is livePrinting the count 0Printing the count 1Main thread will be alive till the child thread is livePrinting the count 2Main thread will be alive till the child thread is livePrinting the count 3Printing the count 4Main thread will be alive till the child thread is livePrinting the count 5Main thread will be alive till the child thread is livePrinting the count 6Printing the count 7Main thread will be alive till the child thread is livePrinting the count 8Main thread will be alive till the child thread is livePrinting the count 9mythread run is overMain thread run is over線程優(yōu)先級(jí)
- 線程優(yōu)先級(jí)是決定一個(gè)線程如何對(duì)待其他線程的整數(shù)。
 - 線程優(yōu)先級(jí)決定何時(shí)從一個(gè)正在運(yùn)行的線程切換到另一個(gè)線程,進(jìn)程稱(chēng)為上下文切換
 - 線程可以自動(dòng)釋放控制,準(zhǔn)備運(yùn)行的最高優(yōu)先級(jí)線程是給定CPU的。
 - 一個(gè)線程可以被一個(gè)高優(yōu)先級(jí)線程搶占,不管低優(yōu)先級(jí)線程在做什么。當(dāng)高優(yōu)先級(jí)線程想要運(yùn)行時(shí),它就會(huì)運(yùn)行。
 - 要設(shè)置線程的優(yōu)先級(jí),使用setPriority()方法,它是線程類(lèi)的一個(gè)方法。
 - 我們可以使用MIN_PRIORITY、NORM_PRIORITY或MAX_PRIORITY來(lái)代替在整數(shù)中定義優(yōu)先級(jí)。
 
方法: isAlive() 和 join()
- 在所有實(shí)際情況下,主線程應(yīng)該是最后一個(gè)完成,其他從主線程派生的線程也會(huì)完成。
 - 要知道線程是否已經(jīng)完成,我們可以在線程上調(diào)用isAlive(),如果線程沒(méi)有完成,它將返回true。
 - 另一種方法是使用join()方法,當(dāng)從父線程調(diào)用該方法時(shí),該方法使父線程等待子線程終止。
 - 這些方法是在Thread類(lèi)中定義的。
 - 在上面的例子中,我們也使用了isAlive()方法。
 
同步
- 多線程為程序引入了異步行為。如果一個(gè)線程正在寫(xiě)一些數(shù)據(jù),那么另一個(gè)線程可能正在讀取相同的數(shù)據(jù)。這可能會(huì)帶來(lái)不一致。
 - 當(dāng)兩個(gè)或多個(gè)線程需要訪問(wèn)共享資源時(shí),應(yīng)該以某種方式讓資源一次只被一個(gè)資源使用。實(shí)現(xiàn)這一點(diǎn)的過(guò)程稱(chēng)為同步。
 - 要實(shí)現(xiàn)同步行為,java有同步方法。一旦線程位于同步方法中,其他線程就不能調(diào)用同一對(duì)象上的任何其他同步方法。然后所有其他線程等待第一個(gè)線程從同步塊中出來(lái)。
 - 當(dāng)我們想要同步對(duì)一個(gè)不是為多線程訪問(wèn)而設(shè)計(jì)的類(lèi)的對(duì)象的訪問(wèn)時(shí),并且需要同步訪問(wèn)的方法的代碼對(duì)我們不可用,在這種情況下,我們不能將synchronized添加到適當(dāng)?shù)姆椒ㄖ小T趈ava中,我們對(duì)此有解決方案,將對(duì)這個(gè)類(lèi)定義的方法(需要同步)的調(diào)用以以下方式放入同步塊中。
 
線程間通信
我們有一些java線程可以彼此通信的方法。這些方法是wait()、notify()、notifyAll()。所有這些方法只能從同步方法中調(diào)用。
1)了解同步j(luò)ava有一個(gè)monitor的概念。監(jiān)視器可以看作是一個(gè)只能容納一個(gè)線程的盒子。一旦一個(gè)線程進(jìn)入監(jiān)視器,所有其他線程必須等待該線程退出監(jiān)視器。
2) wait()告訴調(diào)用線程放棄監(jiān)視器并進(jìn)入睡眠狀態(tài),直到其他線程進(jìn)入同一監(jiān)視器并調(diào)用notify()。
3) notify()喚醒同一對(duì)象上調(diào)用wait()的第一個(gè)線程。
notifyAll()喚醒同一對(duì)象上調(diào)用wait()的所有線程。優(yōu)先級(jí)最高的線程將首先運(yùn)行。
總結(jié)
以上是生活随笔為你收集整理的vs如何写多线程_java中的多线程的示例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
                            
                        - 上一篇: 学校驾驶2017电脑版(学校驾驶2016
 - 下一篇: 电脑主题win7美女主题(win7的主题