Java使用Thread和Runnable的線程實(shí)現(xiàn)方法比較
本文實(shí)例講述了Java使用Thread和Runnable的線程實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:
一 使用Thread實(shí)現(xiàn)多線程模擬鐵路售票系統(tǒng)
1 代碼
public class ThreadDemo
{
public static void main( String[] args )
{
TestThread newTh = new TestThread( );
// 一個(gè)線程對象只能啟動(dòng)一次
newTh.start( );
newTh.start( );
newTh.start( );
newTh.start( );
}
}
class TestThread extends Thread
{
private int tickets = 5;
public void run( )
{
while( tickets > 0 )
{
System.out.println( Thread.currentThread().getName( ) + " 出售票 " + tickets );
tickets -= 1;
}
}
}
2 運(yùn)行
Thread-0 出售票 5
Thread-0 出售票 4
Thread-0 出售票 3
Thread-0 出售票 2
Thread-0 出售票 1
Exception in thread "main" java.lang.IllegalThreadStateException
at java.lang.Thread.start(Thread.java:708)
at ThreadDemo.main(ThreadDemo.java:16)
3 說明
一個(gè)線程只能啟動(dòng)一次
二 main方法中產(chǎn)生4個(gè)線程
1 代碼
public class ThreadDemo
{
public static void main(String[]args)
{
// 啟動(dòng)了四個(gè)線程,分別執(zhí)行各自的操作
new TestThread( ).start( );
new TestThread( ).start( );
new TestThread( ).start( );
new TestThread( ).start( );
}
}
class TestThread extends Thread
{
private int tickets = 5;
public void run( )
{
while (tickets > 0)
{
System.out.println(Thread.currentThread().getName() + " 出售票 " + tickets);
tickets -= 1;
}
}
}
2 運(yùn)行
Thread-0 出售票 5
Thread-0 出售票 4
Thread-0 出售票 3
Thread-0 出售票 2
Thread-0 出售票 1
Thread-1 出售票 5
Thread-1 出售票 4
Thread-1 出售票 3
Thread-1 出售票 2
Thread-1 出售票 1
Thread-2 出售票 5
Thread-2 出售票 4
Thread-2 出售票 3
Thread-2 出售票 2
Thread-2 出售票 1
Thread-3 出售票 5
Thread-3 出售票 4
Thread-3 出售票 3
Thread-3 出售票 2
Thread-3 出售票 1
三 使用Runnable接口實(shí)現(xiàn)多線程,并實(shí)現(xiàn)資源共享
1 代碼
public class RunnableDemo
{
public static void main( String[] args )
{
TestThread newTh = new TestThread( );
// 啟動(dòng)了四個(gè)線程,并實(shí)現(xiàn)了資源共享的目的
new Thread( newTh ).start( );
new Thread( newTh ).start( );
new Thread( newTh ).start( );
new Thread( newTh ).start( );
}
}
class TestThread implements Runnable
{
private int tickets = 5;
public void run( )
{
while( tickets > 0 )
{
System.out.println( Thread.currentThread().getName() + " 出售票 " + tickets );
tickets -= 1;
}
}
}
2 運(yùn)行
Thread-0 出售票 5
Thread-0 出售票 4
Thread-0 出售票 3
Thread-0 出售票 2
Thread-0 出售票 1
更多java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java面向?qū)ο蟪绦蛟O(shè)計(jì)入門與進(jìn)階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章
SpringMVC實(shí)現(xiàn)簡單跳轉(zhuǎn)方法(專題)
這篇文章主要介紹了SpringMVC實(shí)現(xiàn)簡單跳轉(zhuǎn)方法(專題),詳細(xì)的介紹了SpringMVC跳轉(zhuǎn)的幾種方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2018-03-03
spring boot教程之產(chǎn)生的背景及其優(yōu)勢
這篇文章主要介紹了spring boot教程之產(chǎn)生的背景及其優(yōu)勢的相關(guān)資料,需要的朋友可以參考下2022-08-08
基于SpringBoot和Vue寫一個(gè)2048小游戲
創(chuàng)建一個(gè)基于 Java Spring Boot 后端和 Vue 前端的 2048 游戲,可以按照以下步驟進(jìn)行,這個(gè)項(xiàng)目將包括后端(用來處理游戲邏輯)和前端(用來顯示游戲界面和與用戶交互),感興趣的小伙伴可以參考本文自己動(dòng)手嘗試一下2024-08-08
深入解析Andoird應(yīng)用開發(fā)中View的事件傳遞
這篇文章主要介紹了深入解析Andoird應(yīng)用開發(fā)中View的事件傳遞,其中重點(diǎn)講解了ViewGroup的事件傳遞流程,需要的朋友可以參考下2016-02-02
idea如何debug看springsecurity的過濾器順序
這篇文章主要介紹了idea如何debug看springsecurity的過濾器順序,文中通過圖文結(jié)合的方式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-04-04
springboot+spring?data?jpa實(shí)現(xiàn)新增及批量新增方式
這篇文章主要介紹了springboot+spring?data?jpa實(shí)現(xiàn)新增及批量新增方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11

