1. 程式人生 > 其它 >Thread之守護執行緒和執行緒安全問題

Thread之守護執行緒和執行緒安全問題

技術標籤:# 多執行緒多執行緒javathread

守護執行緒

在這裡插入圖片描述


package com.hanyxx.thread;
import lombok.SneakyThrows;

/**
 * @author layman
 * @date 2021/2/5
 */
public class ThreadDaemon04 {
    public static void main(String[] args) throws InterruptedException {
        demo04 demo04 = new demo04();
        //當把demo04設定為守護執行緒時,主執行緒結束時,demo04就會結束。
demo04.setDaemon(true); demo04.start(); Thread.sleep(2000); System.out.println("main執行緒結束"); } } class demo04 extends Thread{ @SneakyThrows @Override public void run() { while(true){ System.out.println("time= "+System.
currentTimeMillis()); Thread.sleep(500); } } }

執行緒安全

在這裡插入圖片描述

驗證區域性變數執行緒安全

package com.hanyxx.thread;

import lombok.SneakyThrows;

/**
 * @author layman
 * @date 2021/2/5
 */
public class Demo05 {
    public static void main(String[] args) {
        Demo05Service service = new Demo05Service(
); Demo05ThreadA threadA = new Demo05ThreadA(service); threadA.start(); Demo05ThreadB threadB = new Demo05ThreadB(service); threadB.start(); } } class Demo05Service{ @SneakyThrows public void changeNum(String username){ int number; if("layman".equals(username)){ number = 100; //執行緒睡眠,讓後續執行緒可以修改number的值 Thread.sleep(2000); }else{ number = 200; } System.out.println("username: ["+username+"] number: ["+number+"]"); } } class Demo05ThreadA extends Thread{ private Demo05Service service; public Demo05ThreadA(Demo05Service service){ this.service = service; } @SneakyThrows @Override public void run() { while (true) { Thread.sleep(1000); service.changeNum("layman"); } } } class Demo05ThreadB extends Thread{ private Demo05Service service; public Demo05ThreadB(Demo05Service service){ this.service = service; } @SneakyThrows @Override public void run() { while (true) { Thread.sleep(1000); service.changeNum("菠菜飯糰"); } } }

驗證成員變數執行緒不安全

package com.hanyxx.thread;

import lombok.SneakyThrows;

/**
 * @author layman
 * @date 2021/2/5
 */
public class Demo05 {
    public static void main(String[] args) {
        Demo05Service service = new Demo05Service();
        Demo05ThreadA threadA = new Demo05ThreadA(service);
        threadA.start();
        Demo05ThreadB threadB = new Demo05ThreadB(service);
        threadB.start();
    }
}
class Demo05Service{
    int number;
    @SneakyThrows
    public void changeNum(String username){
        if("layman".equals(username)){
            number = 100;
            //執行緒睡眠,讓後續執行緒可以修改number的值
            Thread.sleep(2000);
        }else{
            number = 200;
        }
        System.out.println("username: ["+username+"] number: ["+number+"]");
    }
}
class Demo05ThreadA extends  Thread{
    private Demo05Service service;
    public Demo05ThreadA(Demo05Service service){
        this.service = service;
    }
    @SneakyThrows
    @Override
    public void run() {
        while (true) {
            Thread.sleep(1000);
            service.changeNum("layman");
        }
    }
}
class Demo05ThreadB extends  Thread{
    private Demo05Service service;
    public Demo05ThreadB(Demo05Service service){
        this.service = service;
    }
    @SneakyThrows
    @Override
    public void run() {
        while (true) {
            Thread.sleep(1000);
            service.changeNum("菠菜飯糰");
        }
    }
}

結論:

  • 區域性變數在方法內部,是私有的。這意味著它永遠是執行緒安全的。
  • 成員變數為類所有,它不是執行緒安全的。
    在這裡插入圖片描述