日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java中的多线程的示例

發布時間:2024/7/23 编程问答 49 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java中的多线程的示例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在討論多線程之前,讓我們先討論線程。線程是進程中輕量級的最小部分,可以與同一進程的其他部分(其他線程)并發運行。線程是獨立的,因為它們都有獨立的執行路徑,這就是為什么如果一個線程中發生異常,它不會影響其他線程的執行。進程的所有線程共享公共內存。同時執行多個線程的過程稱為多線程。

讓我們把討論總結成以下幾點:

1. 多線程的主要目的是同時執行程序的兩個或多個部分,以最大限度地利用CPU時間。多線程程序包含兩個或多個可以并發運行的部分。程序的每個這樣的部分稱為線程。

2. 線程是輕量級子進程,它們共享公共內存空間。在多線程環境中,受益于多線程的程序可以利用最大的CPU時間,使空閑時間保持在最小。

3.線程可以處于以下狀態之一:

新-尚未啟動的線程處于此狀態。

RUNNABLE——在Java虛擬機中執行的線程處于這種狀態。

阻塞——等待監視器鎖的阻塞線程處于這種狀態。

等待——正在無限期等待另一個線程執行特定操作的線程處于這種狀態。

TIMED_WAITING—等待另一個線程執行某個操作長達指定等待時間的線程處于這種狀態。

終止-已退出的線程處于此狀態。

在給定的時間點上,線程只能處于一種狀態。

多任務vs多線程vs多處理vs并行處理

如果您是java新手,您可能會對這些術語感到困惑,因為在我們討論多線程時它們經常使用。讓我們簡單地談一談。

多任務處理:?同時執行多個任務的能力稱為多任務處理。

多線程:?我們已經討論過了。它是一個同時執行多個線程的進程。多線程也稱為基于線程的多任務處理。

多處理:?它與多任務處理相同,但是在多處理中涉及多個cpu。另一方面,一個CPU參與多任務處理。

并行處理:?它是指在一個計算機系統中使用多個cpu。

在用Java創建線程

在Java中有兩種創建線程的方法:

1)通過擴展Thread類。

2)通過實現Runnable接口。

在開始創建線程的程序(代碼)之前,讓我們先看看Thread類的這些方法。在下面的示例中,我們很少使用這些方法。

getName():用于獲取線程的名稱

getPriority():獲取線程的優先級

isAlive():確定線程是否仍在運行

join():等待線程終止

run():線程的入口點

sleep():掛起線程一段時間

start():通過調用線程的run()方法來啟動線程

方法1:通過擴展線程類創建線程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:通過實現Runnable接口創建線程

一個簡單示例

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:

觀察這個程序的輸出,并嘗試理解這個程序中發生了什么。如果您已經理解了每個線程方法的用法,那么您應該不會遇到任何問題,請理解這個示例。

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

線程優先級

線程優先級是決定一個線程如何對待其他線程的整數。

線程優先級決定何時從一個正在運行的線程切換到另一個線程,進程稱為上下文切換

線程可以自動釋放控制,準備運行的最高優先級線程是給定CPU的。

一個線程可以被一個高優先級線程搶占,不管低優先級線程在做什么。當高優先級線程想要運行時,它就會運行。

要設置線程的優先級,使用setPriority()方法,它是線程類的一個方法。

我們可以使用MIN_PRIORITY、NORM_PRIORITY或MAX_PRIORITY來代替在整數中定義優先級。

方法: isAlive() 和 join()

在所有實際情況下,主線程應該是最后一個完成,其他從主線程派生的線程也會完成。

要知道線程是否已經完成,我們可以在線程上調用isAlive(),如果線程沒有完成,它將返回true。

另一種方法是使用join()方法,當從父線程調用該方法時,該方法使父線程等待子線程終止。

這些方法是在Thread類中定義的。

在上面的例子中,我們也使用了isAlive()方法。

同步

多線程為程序引入了異步行為。如果一個線程正在寫一些數據,那么另一個線程可能正在讀取相同的數據。這可能會帶來不一致。

當兩個或多個線程需要訪問共享資源時,應該以某種方式讓資源一次只被一個資源使用。實現這一點的過程稱為同步。

要實現同步行為,java有同步方法。一旦線程位于同步方法中,其他線程就不能調用同一對象上的任何其他同步方法。然后所有其他線程等待第一個線程從同步塊中出來。

當我們想要同步對一個不是為多線程訪問而設計的類的對象的訪問時,并且需要同步訪問的方法的代碼對我們不可用,在這種情況下,我們不能將synchronized添加到適當的方法中。在java中,我們對此有解決方案,將對這個類定義的方法(需要同步)的調用以以下方式放入同步塊中。

Synchronized(object){

// statement to be synchronized}

線程間通信

我們有一些java線程可以彼此通信的方法。這些方法是wait()、notify()、notifyAll()。所有這些方法只能從同步方法中調用。

1)了解同步java有一個monitor的概念。監視器可以看作是一個只能容納一個線程的盒子。一旦一個線程進入監視器,所有其他線程必須等待該線程退出監視器。

2) wait()告訴調用線程放棄監視器并進入睡眠狀態,直到其他線程進入同一監視器并調用notify()。

3) notify()喚醒同一對象上調用wait()的第一個線程。

notifyAll()喚醒同一對象上調用wait()的所有線程。優先級最高的線程將首先運行。

?為了讓學習變得輕松、高效,今天給大家免費分享一套Java教學資源。幫助大家在成為Java架構師的道路上披荊斬棘。需要資料的歡迎加入學習交流群:9285,05736

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的java中的多线程的示例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。