Java高并发-多线程基础
1.多線程基礎
中斷線程
public void Thread.interrupt() //中斷線程
public boolean Thread.isInterrupted() //判斷是否被中斷
public static boolean Thread.interrupted() //判斷是否被中斷,并清除當前中斷中斷
線程的基本操作
中斷線程
public void run(){
???????? while(true){
?????????????????? Thread.yeild();
???????? }
}
t1.interrupt();
public void run(){
???????? while(true){
?????????????????? if(Thread.currentThread().isInterrupted()){
??????????????????????????? System.out.println("Interruted!");
?????????????????? ???????? break;
?????????????????? }
?????????????????? Thread.yield();
???????? }
}
Java線程中的Thread.yield( )方法,譯為線程讓步。顧名思義,就是說當一個線程使用了這個方法之后,它就會把自己CPU執行的時間讓掉,
讓自己或者其它的線程運行,注意是讓自己或者其他線程運行,并不是單純的讓給其他線程。
??? yield()的作用是讓步。它能讓當前線程由“運行狀態”進入到“就緒狀態”,從而讓其它具有相同優先級的等待線程獲取執行權;但是,并不能保證在當前線程調用yield()之后,其它具有相同優先級的線程就一定能獲得執行權;也有可能是當前線程又進入到“運行狀態”繼續運行!
線程的基本操作--線程中斷
public static native void sleep(long mills) throws InterruptedException
public void run(){
???????? while(true){
?????????????????? if(Thread.currentThread().isInterrupted()){
??????????????????????????? System.out.println("Interrupted!");
??????????????????????????? break;
?????????????????? }
?????????????????? try {
??????????????????????????? Thread.sleep(2000);
?????????????????? } catch (InterruptedException e){
??????????????????????????? System.out.println("Interrupted When Sleep");
??????????????????????????? //設置中斷狀態,拋出異常后會清除中斷標記位
??????????????????????????? Thread.currentThread().interrupt();
?????????????????? }
?????????????????? Thread.yield();
???????? }
}
掛起(suspend)和繼續執行(resume)線程
-? suspend()不會釋放鎖
-? 如果加鎖發生在resume()之前,則死鎖發生
stop()、suspend()和resume()方法不推薦使用
?
等待線程結束(join)和謙讓(yeild)
public final void join() throws InterruptedException
public final synchronized void join(long millis) throws InterruptedException
public class JoinMain {
???????? public volatile static int i = 0;
???????? public static class AddThread extends Thread {
?????????????????? @Override
?????????????????? public void run(){
??????????????????????????? for(i = 0; i < 10000000; i++);
?????????????????? }
???????? }
???????? public static void main(String[] args) throws InterruptedException {
?????????????????? AddThread at = new AddThread();
?????????????????? at.start();
?????????????????? at.join();
?????????????????? System.out.println(i);
???????? }
}
join的本質???????????????????????? 線程執行完畢后
while(isAlive()){???????? ???????? 系統會調用
???????? wait(0);???????????? ???????? notifyAll()
}
不要在Thread實例上使用wait()和notify()方法
守護線程
在后臺默默地完成一些系統性的服務,比如垃圾回收線程、JIT線程就可以理解為守護線程
當一個Java應用內,只有守護線程時,Java虛擬機就會自然退出
Thread t = new DaemonT();
t.setDaemon(true);
t.start();
線程優先級
高優先級的線程更容易在競爭中獲勝
基本的線程同步操作
synchronized
- 指定加鎖對象:對給定對象加鎖,進入同步代碼前要獲得給定對象的鎖。
- 直接作用于實例方法:相當于對當前實力加鎖,進入同步代碼前要獲得當前實例的鎖。
-直接作用于靜態方法:相當于對當前類加鎖,進入同步代碼前要獲得當前類的鎖。
Object.wait() Object.notify()
會釋放鎖
指定加鎖對象
public void run(){
???????? FOR(int j = 0; j < 100000000; j+++){
?????????????????? synchronized(instance){
??????????????????????????? i++;
?????????????????? }
???????? }
}
實例方法同步
public synchronized void increase(){
???????? i++;
}
靜態方法同步
public static synchronized void increase(){
???????? i++;
}
參考《實戰Java高并發程序設計》
總結
以上是生活随笔為你收集整理的Java高并发-多线程基础的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 百度地图1
- 下一篇: awt jtable 多线程加载图片_J