溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

初學Java多線程的基本概念

發(fā)布時間:2020-05-27 03:57:28 來源:網(wǎng)絡 閱讀:139 作者:涼白開dream 欄目:編程語言

一:線程與之前所學的進程區(qū)別:
進程:系統(tǒng)進行資源分配的最小單位,資源是獨立的
線程:CPU調度的最小的單位,線程間共享資源。多線程幾個線程共占據(jù)一個進程,單線程獨占一個線程。

主線程就像是前臺接待,當工作量太多時,就指揮子線程去完成對應工作。

二,如何創(chuàng)建線程
繼承Thread類和實現(xiàn)Runnable接口。
在使用Runnable接口時需要建立一個Thread實例。因此,無論是通過Thread類還是Runnable接口建立線程,都必須建立Thread類或它的子類的實例。

Thread構造函數(shù):

public Thread( );
public Thread(Runnable target);
public Thread(String name);
public Thread(Runnable target, String name);
public Thread(ThreadGroup group, Runnable target);
public Thread(ThreadGroup group, String name);
public Thread(ThreadGroup group, Runnable target, String name);
public Thread(ThreadGroup group, Runnable target, String name, long stackSize);

第一類:

public class ThreadDemo{
     public static void main(String[] args){
         Demo d = new Demo();//創(chuàng)建多線程
         d.start();//啟動線程
         for(int i=0;i<60;i++){
             System.out.println(Thread.currentThread().getName()+i);//打印當前線程名稱
         }

     }
 }
 class Demo extends Thread{//繼承thread類
     public void run(){//run方法相當于線程的行動指南(做什么工作)
         for(int i=0;i<60;i++){
             System.out.println(Thread.currentThread().getName()+i);//打印名字
         }
     }
 }

第二類:
public class ThreadDemo{
public static void main(String[] args){
Demo d =new Demo();
Thread t = new Thread(d);//Thread類或它的子類的實例。
t.start();
for(int x=0;x<60;x++){
System.out.println(Thread.currentThread().getName()+x);
}
}
}
class Demo implements Runnable{
public void run(){
for(int x=0;x<60;x++){
System.out.println(Thread.currentThread().getName()+x);
}
}
}
三:如何終止線程。

  1. 使用退出標志,使線程正常退出,也就是當run方法完成后線程終止。

  2. 使用stop方法強行終止線程(這個方法不推薦使用,可能發(fā)生不可預料的結果)。

  3. 使用interrupt方法中斷線程。
    舉例子:A通知B A.interrupt() A將指示燈置亮,B發(fā)生中斷,(如果B睡著的話,會存在通知不及時的情況)
    B如何查看指示燈狀態(tài)。Thread.interrupted()和Thread.isinterrupted()

    Thread.isinterrupted()會將指示燈復位,置回原來的狀態(tài)。可查看任意線程的狀態(tài)。
    Thread.interrupted()不會將指示燈復位,只可查看當前線程的狀態(tài)。

    四:多線程安全問題
    問題原因:當多條語句在操作同一個線程共享數(shù)據(jù)時,一個線程對多條語句只執(zhí)行了一部分,還沒執(zhí)行完,另一個線程參與進來執(zhí)行,導致共享數(shù)據(jù)的錯誤。

解決辦法:對多條操作共享數(shù)據(jù)的語句,只能讓一個線程都執(zhí)行完,在執(zhí)行過程中,其他線程不執(zhí)行。

兩個關鍵字:
volatile修飾變量,實現(xiàn)可見性,當線程的工作內存數(shù)據(jù)改變,主內存也隨之改變,
synchronized修飾方法,實現(xiàn)加鎖功能,當CPU發(fā)生調度時,保證自己的工作不會受別人打擾。
同步代碼塊:


public class ThreadDemo3 {
    public static void main(String[] args){
        Ticket t =new Ticket();
        Thread t1 = new Thread("線程一");
        Thread t2 = new Thread("線程二");
        Thread t3 = new Thread("線程三");
        Thread t4 = new Thread("線程四");
        t1.start();
        t2.start();
        t3.start();
        t4.start();
    }
}
class Ticket implements Runnable{
    private int ticket =400;
    public void run(){
        while(true){
            synchronized (new Object()) {
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
    e.printStackTrace();
                }
                if(ticket<=0)
                    break;
                System.out.println(Thread.currentThread().getName()+ticket--);
            }
        }
    }
}

同步函數(shù):
public class ThreadDemo3 {
    public static void main(String[] args){
        Ticket t =new Ticket();
          Thread t1 = new Thread("線程一");
        Thread t2 = new Thread("線程二");
        Thread t3 = new Thread("線程三");
        Thread t4 = new Thread("線程四"););
        t1.start();
        t2.start();
        t3.start();
        t4.start();
    }
}
class Ticket implements Runnable{
    private int ticket = 4000;
    public synchronized void  saleTicket(){
        if(ticket>0)
            System.out.println(Thread.currentThread().getName()+ticket--);

    }
    public void run(){
        while(true){
            saleTicket();
        }
    }
}
向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。

AI