Java線程實(shí)現(xiàn)的兩種方式解析
Java線程實(shí)現(xiàn)的兩種方式解析
1.通過(guò)繼承thread,得到一個(gè)任務(wù)類(lèi)
/*注意在構(gòu)造器中啟動(dòng)這個(gè)線程的話,很容易造成this逃逸的問(wèn)題,這是要注意的
* 這是通過(guò)直接集成thread來(lái)成為線程。同時(shí)在這種情況下,你可以通過(guò)調(diào)用合適的方法來(lái)
* 給thread對(duì)象賦予具體的名稱。*/
public class SimpleThread extends Thread {
private int countDown=5;
private static int threadCount=0;
public SimpleThread() {
super(Integer.toString(++threadCount));//這是給這個(gè)thread賦予名字。
start();
}
public String toString(){
return "#"+getName()+"("+countDown+"), ";
}
public void run() {
while (true) {
System.out.print(this);
if (--countDown == 0) {
return;
}
}
}
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
new SimpleThread();
}
}
}運(yùn)行的結(jié)果為:
#1(5),
#2(5),
#3(5),
#3(4),
#3(3),
#3(2),
#4(5),
#2(4),
#5(5),
#5(4),
#1(4),
#5(3),
#5(2),
#2(3),
#2(2),
#2(1),
#4(4),
#4(3),
#4(2),
#4(1),
#3(1),
#5(1),
#1(3),
#1(2),
#1(1),
2.通過(guò)實(shí)現(xiàn)Runnable接口,來(lái)得到一個(gè)任務(wù)類(lèi)
//注意,Start()是在構(gòu)造器中調(diào)用的。這個(gè)實(shí)例相當(dāng)?shù)暮?jiǎn)單,因此可能是安全的。但是應(yīng)該注意到
//在構(gòu)造器重啟動(dòng)線程可能會(huì)變得很有問(wèn)題。因?yàn)榱硗庖粋€(gè)任務(wù)可能會(huì)在構(gòu)造器結(jié)束以前就開(kāi)始執(zhí)行了
//這意味著該任務(wù)能夠訪問(wèn)處于不穩(wěn)定狀態(tài)的對(duì)象。這是優(yōu)選Executor而不是顯式地創(chuàng)建Thread對(duì)象的
//另外一個(gè)很重要的原因。
public class SelfManaged implements Runnable {
private int countDown=5;
private Thread t = new Thread(this);
public SelfManaged() {
t.start();
}
public String toString(){
return Thread.currentThread().getName()+"("+countDown+")";
}
@Override
public void run() {
while (true) {
System.out.println(this);
if (--countDown == 0) {
return;
}
}
}
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
new SelfManaged();
}
}
}最后運(yùn)行的結(jié)果為:
Thread-0(5) Thread-2(5) Thread-1(5) Thread-1(4) Thread-2(4) Thread-2(3) Thread-2(2) Thread-2(1) Thread-0(4) Thread-0(3) Thread-0(2) Thread-0(1) Thread-3(5) Thread-1(3) Thread-3(4) Thread-4(5) Thread-4(4) Thread-4(3) Thread-4(2) Thread-4(1) Thread-1(2) Thread-1(1) Thread-3(3) Thread-3(2) Thread-3(1)
3.為線程設(shè)置優(yōu)先級(jí)
但是設(shè)置優(yōu)先級(jí)只是一個(gè)建議。具體是否執(zhí)行的話,還是要看系統(tǒng)內(nèi)部的調(diào)度。
public class SimplePriorities implements Runnable {
private int countDown=5;
private volatile double d;
private int priority;
public SimplePriorities(int priority) {
this.priority=priority;
}
public String toString(){
return Thread.currentThread()+":"+countDown;
}
@Override
public void run() {
Thread.currentThread().setPriority(priority);//為這個(gè)線程設(shè)置優(yōu)先級(jí)。
while (true) {
for (int i = 0; i < 10000; i++) {
d += (Math.PI + Math.E)/(double)i;
if (i / 1000 == 0) {
Thread.yield();
}
}
System.out.println(this);
if (--countDown == 0) {
return;
}
}
}
public static void main(String[] args) {
ExecutorService exec= Executors.newCachedThreadPool();
for (int i = 0; i < 5; i++) {
exec.execute(new SimplePriorities(Thread.MAX_PRIORITY));
exec.execute(new SimplePriorities(Thread.MIN_PRIORITY));
exec.shutdown();
}
}
}4.繼承thread和實(shí)現(xiàn)runnable之間的區(qū)別
1、如是是實(shí)現(xiàn)了Runnable接口的話,那么就為實(shí)現(xiàn)多繼承提供了方便。因?yàn)閖ava中,只允許單繼承。
2、實(shí)現(xiàn)runnable接口可以實(shí)現(xiàn)資源的共享。
這就是它們之間的最大的區(qū)別。
下面以一個(gè)買(mǎi)票的例子來(lái)說(shuō)明它們之間的區(qū)別。
class MyThread extends Thread{
private int ticket=10;
public void run(){
for(int i=0;i<20;i++){
if(this.ticket>0){
System.out.println("賣(mài)票:ticket"+this.ticket--);
}
}
}
};
下面通過(guò)三個(gè)線程對(duì)象,同時(shí)賣(mài)票:
package org.demo.dff;
public class ThreadTicket {
public static void main(String[] args) {
MyThread mt1=new MyThread();
MyThread mt2=new MyThread();
MyThread mt3=new MyThread();
mt1.start();//每個(gè)線程都各賣(mài)了10張,共賣(mài)了30張票
mt2.start();//但實(shí)際只有10張票,每個(gè)線程都賣(mài)自己的票
mt3.start();//沒(méi)有達(dá)到資源共享
}
}
package org.demo.runnable;
class MyThread implements Runnable{
private int ticket=10;
public void run(){
for(int i=0;i<20;i++){
if(this.ticket>0){
System.out.println("賣(mài)票:ticket"+this.ticket--);
}
}
}
}
package org.demo.runnable;
public class RunnableTicket {
public static void main(String[] args) {
MyThread mt=new MyThread();
new Thread(mt).start();//同一個(gè)mt,但是在Thread中就不可以,如果用同一
new Thread(mt).start();//個(gè)實(shí)例化對(duì)象mt,就會(huì)出現(xiàn)異常
new Thread(mt).start();
}
};
到此這篇關(guān)于Java線程實(shí)現(xiàn)的兩種方式解析的文章就介紹到這了,更多相關(guān)Java線程實(shí)現(xiàn)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java 實(shí)現(xiàn)多線程的方法總結(jié)
這篇文章主要介紹了java 實(shí)現(xiàn)多線程的方法總結(jié)的相關(guān)資料,需要的朋友可以參考下2016-10-10
深入淺析SPI機(jī)制在JDK與Spring?Boot中的應(yīng)用
SPI是一種使軟件框架或庫(kù)更加模塊化、可擴(kuò)展和可維護(hù)的有效方法。通過(guò)遵循“開(kāi)閉原則”,?SPI?確保了系統(tǒng)的穩(wěn)定性和靈活性,從而滿足了不斷變化的業(yè)務(wù)需求,這篇文章主要介紹了SPI機(jī)制在JDK與Spring?Boot中的應(yīng)用,需要的朋友可以參考下2023-09-09
10個(gè)Elasticsearch查詢的實(shí)用技巧分享
Elasticsearch是一個(gè)非常流行的搜索引擎,已經(jīng)成為了許多企業(yè)的首選解決方案。本文將向大家介紹10個(gè)實(shí)用的Elasticsearch查詢技巧,并配上對(duì)應(yīng)的代碼示例,希望對(duì)大家有所幫助2023-04-04
解決IDEA中同項(xiàng)目引用報(bào)紅問(wèn)題
在IDEA中,如果項(xiàng)目引用報(bào)紅,可能是因?yàn)镮DEA的引用緩存問(wèn)題,可以通過(guò)File->Invalidate Caches/Restart清空緩存并重建索引來(lái)解決,這個(gè)方法可以幫助解決同項(xiàng)目中引用找不到的問(wèn)題,恢復(fù)正常的項(xiàng)目引用,消除報(bào)紅2024-09-09
SpringBoot基于攔截器與ThreadLocal實(shí)現(xiàn)用戶登錄校驗(yàn)
本文介紹了一種在SpringBoot中實(shí)現(xiàn)統(tǒng)一登錄校驗(yàn)和上下文共享的解決方案,通過(guò)使用攔截器、ThreadLocal和自定義工具類(lèi),實(shí)現(xiàn)了在請(qǐng)求處理過(guò)程中優(yōu)雅地獲取當(dāng)前登錄用戶信息,并且避免了在每個(gè)Controller方法中重復(fù)編寫(xiě)校驗(yàn)代碼,需要的朋友可以參考下2025-12-12

