對執行緒的簡單理解

2020-10-19 11:00:28

當多個執行緒共用一份資料,執行相同任務的時候,會發生執行緒安全問題
當四個執行緒執行start()方法以後,這時候,CPU是隨機的,四個執行緒同時搶CPU,如果thread1搶到,當他用完時間片,四個執行緒再一起搶時間片,這時候也不一定誰能搶到,當時間片用完的時候,需要立即釋放,那麼可能會停在執行任務的run方法裡面,假設停在了 ticket.num = ticket.num-1;這,等thread2 ,thread3,thread4也停在這裡,這樣等執行緒再搶到時間片的時候,就會發生資料錯誤的情況,這樣就繁盛了執行緒不安全的情況,這樣就可以用鎖解決

public class Demo1 {
    public static void main(String[] args) {
        Sell sell = new Sell();
        //四個執行緒,把執行緒和任務繫結在一起
        Thread thread1 = new Thread(sell);
        Thread thread2 = new Thread(sell);
        Thread thread3 = new Thread(sell);
        Thread thread4 = new Thread(sell);
        thread1.start();
        thread2.start();
        thread3.start();
        thread4.start();
    }
}
//四個執行緒共用一份資料
class Ticket{
    int num = 2000;
}
//這個是賣票任務
class Sell implements Runnable{
   Ticket ticket = new Ticket();
    @Override
    public void run() {
        while (true){
            ticket.num = ticket.num-1;//假設四個執行緒用完時間片都停在了這,當再次搶到時間片的時候,資料就會發生錯誤
            System.out.println(Thread.currentThread().getName()+"賣票了"+"還剩"+ticket.num+"張票");
        }
    }
}

我們需要給加一把鎖,並且必須他們四個共用的鎖,這時候就能避免當thread1 執行緒用完時間片,但是他還是在鎖裡面,雖然他釋放了時間片,但是必須等他再次搶到CPU的時候,全都執行完這個程式碼,這時候其他三個執行緒才有資格進來,能解決執行緒安全的問題

class Sell implements Runnable{
   Ticket ticket = new Ticket();
    @Override
    public void run() {
        while (true){
            synchronized (ticket){
                ticket.num = ticket.num-1;
                System.out.println(Thread.currentThread().getName()+"賣票了"+"還剩"+ticket.num+"張票");
            }
        }
    }
}

上鎖可以上位元組碼檔案物件,兩個共有的,還有this
當我們有一份資料,兩個執行緒和兩個任務的時候,是列印和輸出任務
這時候我應該列印出來 name:zhangage:19或者 name:liage:20,但是出來的時候有時候年齡和姓名不匹配也是發生了執行緒安全問題,當
thread1 搶到了時間片,可能是卡在了age 和name 那,等到thread2搶到時間片的時候,列印就是錯誤的結果,這時候我們應該給thread1 加鎖,但是列印thread2 也有可能出現執行緒安全問題,所以列印年齡和名字還是 不匹配,這樣的情況我們就應該給Syst 和Print 同時上一把鎖,這樣person 就是最合適的

public class Demo2 {
    public static void main(String[] args) {
        Person person = new Person("zhang",20);
        Print print = new Print(person);
        Syst syst  = new Syst(person);
        Thread thread1 = new Thread(print);
        Thread thread2 = new Thread(syst);
        thread1.start();
        thread2.start();
    }
}
class Person{
    String name;
    int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
}
class Print implements Runnable{
    int i = 1;
    Person person;

    public Print(Person person) {
        this.person = person;
    }

    @Override
    public void run() {
        while (true){
            synchronized (person){
                if(i%2==0){
                    person.name = "zhang";
                    person.age = 19;
                }else{
                    person.name = "li";
                    person.age = 20;
                }
                i = i+1;
            }

        }

        }
    }

class Syst implements Runnable{
    Person person ;

    public Syst(Person person) {
        this.person = person;
    }

    @Override
    public void run() {
        while (true){
            synchronized (person){
                System.out.println(Thread.currentThread().getName()+"  " +"name:"+person.name+"age:"+person.age);
            }

        }
    }
}

但是對於列印來說,應該是一次輸入一次輸出才對,所以就要寫喚醒等待機制,當thread1 可以搶CPU的時候,thread2等待,暫時 失去搶CPU的能力,當thread1執行完後,他就進入了wait,thread2搶到CPU執行任務,這樣就會交替進行

class Person1{
    String name;
    int age;
    boolean flag = false;//用於執行喚醒等待的切換
    public Person1(String name, int age) {
        this.name = name;
        this.age = age;
    }
}
class Print1 implements Runnable{
    int i = 1;
    Person1 person;
    public Print1(Person1 person) {
        this.person = person;
    }
    @Override
    public void run() {
        while (true){
            synchronized (person){
                if(person.flag==true){
                    try {
                        person.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                if(i%2==0){
                    person.name = "zhang";
                    person.age = 19;
                }else{
                    person.name = "li";
                    person.age = 20;
                }
                i = i+1;
                person.flag = !person.flag;
                person.notify();
            }

        }

        }
    }

class Syst1 implements Runnable{
    Person1 person ;
    public Syst1(Person1 person) {
        this.person = person;
    }
    @Override
    public void run() {
        while (true){
            synchronized (person){
                if(person.flag==false){
                    try {
                        person.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println(Thread.currentThread().getName()+"  " +"name:"+person.name+"age:"+person.age);
                person.flag = !person.flag;
                person.notify();
            }

        }
    }
}

我們除了可以用喚醒等待機制意外,還可以用Lock和Condition 搭配使用,在單消費者和單生產者中,用哪個都一樣,但是到了多生產者和多消費者的情況下,如果使用synchronized 會用到 notifyAll();這樣會把對方全部的執行緒喚醒,

public class Demo5 {
    public static void main(String[] args) {
        Person3 person = new Person3("zhang",20);
        Print3 print = new Print3(person);
        Syst3 syst  = new Syst3(person);
        Thread thread1 = new Thread(print);
        Thread thread2 = new Thread(syst);
        thread1.start();
        thread2.start();
    }
}
class Person3{
    int  i = 1;
    String name;
    int age;
    boolean flag = false;//用於執行喚醒等待的切換
    Lock lock = new ReentrantLock();//相當於synchronized  他有lock()和unlock()方法
    Condition preCon = lock.newCondition();//有await()等待和signal()喚醒方法
    Condition sysCon = lock.newCondition();
    public Person3(String name, int age) {
        this.name = name;
        this.age = age;
    }
    public void getData(){
           try {
               lock.lock();
               while (flag==false){
                   try {
                       preCon.await();
                   } catch (InterruptedException e) {
                       e.printStackTrace();
                   }
               }
               System.out.println(Thread.currentThread().getName()+"  " +"name:"+name+"age:"+age);
               flag = !flag;
               sysCon.signal();
           }finally {

               lock.unlock();
           }
    }
    public void setData(){
      try {
              lock.lock();
                  while (flag==true){
                      try {
                          sysCon.await();
                      } catch (InterruptedException e) {
                          e.printStackTrace();
                      }
                  }
                  if(i%2==0){
                      name = "zhang";
                      age = 19;
                  }else{
                      name = "li";
                      age = 20;
                  }
           i = i+1;
          flag = !flag;
          preCon.signal();

  }finally {
      lock.unlock();
  }
    }
}

class Print3 implements Runnable{
    int i = 1;
    Person3 person;
    public Print3(Person3 person) {
        this.person = person;
    }
    @Override
    public void run() {
        while (true){
            person.setData();
        }
        }
    }
class Syst3 implements Runnable{
    Person3 person ;
    public Syst3(Person3 person) {
        this.person = person;
    }
    @Override
    public void run() {
        while (true){
            person.getData();
        }
    }
}