一、建立執行緒的方法一:
繼承:extends thread
重寫run()方法
舉個栗子?:
public class mythread extends thread
//傳遞name表示執行緒名字
public mythread(string name)
@override
public void run()
system.out.println(getname()+":"+i);
} }}
mythread t1 = new mythread(「執行緒一」);
mythread t2 = new mythread(「執行緒二」);
thread thread = new thread();
thread.start();//啟動執行緒
thread.getpriority();//獲取優先順序
thread.setpriority();//設定優先順序(1-10)
thread .getname();//獲取執行緒名字
thread .setname();//設定執行緒名字
執行緒休眠:
thread.sleep(4000);//主線程休眠四秒之後再執行
執行緒加入:
t1.join();//將執行緒t1加入主線程
設定守護執行緒:
執行緒的中斷:
t1.interrupt();//手動處理執行緒中斷
interrupted();//將返回布林值
執行緒的生命週期:
**實現runnable介面:**implement runnable
public class runnablethread implements runnable }}
該方法不能直接使用start()方法啟動執行緒。需要使用thread宣告乙個物件,再將runnable物件傳入thread物件。
runnablethread t = new runnablethread();
thread t1 = new thread(t,」執行緒一」);
t1.start();
thread t2 = new thread(t,」執行緒二」);
t2.start();
兩種建立執行緒方法區別:使用runnable方法建立執行緒的好處:runnable方法只需要建立乙個runnable物件,thread方法需要建立多個mythread物件。thread方法獲取執行緒其他引數更方便。兩種方法都需要重寫run()方法。
可以很方便的實現多執行緒資料的共享。
該方法還可以繼承其他的類,而繼承thread類的方法不能再繼承其他的類了。
**匿名內部類:**直接在主類裡面定義乙個runnable類來實現runnable介面裡面的run()方法。
適用於該執行緒在主類裡面只使用一次的情況,不用再建立乙個執行緒類來實現runnable介面。
**執行緒安全問題:**當多個執行緒需要同時修改同一共享資料時,產生衝突,就會出現執行緒安全問題。
加鎖解決:
private object lock = new object();//需要宣告乙個物件作為鎖
//加鎖
synchronized (lock) //執行完畢,歸還鑰匙
也可以直接使用同步方法:sellticket()
public synchronized void sellticket()//相當於給這個方法加了鎖
同步方法不需要再宣告乙個物件作為鎖了。
使用專門的鎖物件reentrantlock來加鎖:
private reentrantlock lock = new reentrantlock();
lock.lock();//加鎖
//需要加鎖的**
lock.unlock();//解鎖
死鎖問題的解決:
多個執行緒以相同的順序去取鎖,取到所有鎖之後才執行需要加鎖的**!
舉個栗子?:
public class deadlock
}//匿名內部類實現runnable建立thread1執行緒
class thread1 implements runnable catch (interruptedexception e)
synchronized (deadlock.lock2)
} } }
//匿名內部類實現runnable建立thread2執行緒
建立執行緒組:threadgroup
threadgroup tg = new threadgroup(「我的執行緒組」);
定時器:timer需要在計時器任務裡面設定任務:timertask
timer timer = new timer();//定時器
timer.schedule(new mytimer(), 2000);//2s後啟動計時器任務
timer.schedule(new mytimer(), 2000, 1000);//2s啟動後,隔3s後再重複執行計時器任務
timer.cancel();//取消定時器
舉個栗子?:
public class demo_timer
}//定時器任務(匿名內部類)
電影院售票問題:採用實現runnable介面的方法建立執行緒。
private object lock;
this.lock = lock; }
@override
public void run()
}try catch (interruptedexception e)
} }}
2、建立windowthread類:(window端線程類)
public class windowthread implements runnable
@override
public void run()
}try catch (interruptedexception e) }}}
3、建立movieticketmanage類:(票管理類,儲存電影票數量,為了多執行緒資料的共享)
public class movieticketmanage
複製**4、建立cinemasale主類:(電影票售票主類)
public class cinemasale
}
去做那些讓你緊張的事情,當你克服了這段焦慮之後,你會發現,一切沒你想象的那麼困難。? Java多執行緒學習(1) 停止執行緒
呼叫interrupt方法只是對執行緒做了乙個標記 停止標記 並沒有停止執行緒的效果,需要結合以下兩種方法 如果呼叫了interrupt 方法,interrupted 返回true,看乙個例子 threadtest thread new threadtest thread.start thread....
Java 多執行緒學習總結2
併發執行最理想的狀況是讓一些 毫不相干 的somebody 自己做自己的事情,和別人無關。但是事實讓確非如此,因為在哲學上講,世界是乙個整體,每個個體之間都存在聯絡,我們讓一些東西不相干,是我們人類為了簡化問題,而從概念上進行的分割劃分,呵呵。事實上,如果某個somebody 為多個執行緒共享,其中...
Java 多執行緒學習總結3
執行緒的死鎖 如果多個執行緒操作多個物件互相請求,那麼會引起死鎖。package deadlock public class somebody public synchronized void saysomething catch interruptedexception e other.sings...