国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home 類庫(kù)下載 java類庫(kù) [Effective Java] Synchronous access to shared mutable data

[Effective Java] Synchronous access to shared mutable data

Oct 10, 2016 am 08:57 AM

I feel like there is nothing that needs to be recorded during this period of time, and I personally don’t have any ideas, but I will still have to remember and write more in the future

package cn.xf.cp.ch02.item66;

import java.util.concurrent.TimeUnit;

import org.junit.Test;

public class StopThread
{
    /**
     * 停止線程變量
     */
    private static boolean stopRequested;
    
    //吧對(duì)變量的讀和寫方法都進(jìn)行同步
    private static synchronized void requestStop()
    {
        stopRequested = true;
    }
    
    private static synchronized boolean stopRequested()
    {
        return stopRequested;
    }
    
    /**
     * 停止線程變量,這個(gè)使用關(guān)鍵字volatile使每個(gè)線程都是獲取到最新的值
     */
    private static volatile boolean stopRequested2;
    
    @Test
    public void test()
    {
        Thread backgroundThread = new Thread(new Runnable()
        {
            @Override
            public void run()
            {
                int i = 0;
                while(!stopRequested())
                {
                    ++i;
                }
            }
        });
        //啟動(dòng)線程
        backgroundThread.start();
        
        //休眠1秒
        try
        {
            TimeUnit.SECONDS.sleep(1);
        }
        catch (InterruptedException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        requestStop();
    }
    
    @Test
    public void test2()
    {
        Thread backgroundThread = new Thread(new Runnable()
        {
            @Override
            public void run()
            {
                int i = 0;
                System.out.println("這里使用最新的stopRequested2值");
                while(!stopRequested2)
                {
                    ++i;
                }
            }
        });
        
        //啟動(dòng)線程
        backgroundThread.start();
        
        //停下1s之后執(zhí)行變量修改程序
        try
        {
            TimeUnit.SECONDS.sleep(1);
        }
        catch (InterruptedException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //修改變量
        stopRequested2 = true;
    }
    
    
    public static void main(String[] args) throws InterruptedException
    {
        Thread backgroundThread = new Thread(new Runnable()
        {
            @Override
            public void run()
            {
                int i = 0;
                System.out.println(stopRequested); //false
                //這里停不下來(lái)是因?yàn)橹骶€程對(duì)stopRequest進(jìn)行修改的時(shí)候,這個(gè)線程并不可見(jiàn)
                while(!stopRequested)
                {
                    ++i;
                }
            }
        });
        //啟動(dòng)線程
        backgroundThread.start();
        
        //休眠1秒
        TimeUnit.SECONDS.sleep(1);
        stopRequested = true;
    }
}

This main method will never stop, and the other two are from two different angles Synchronization methods are given

In short: when multiple threads share variable data, each thread that reads or writes data must perform synchronization.


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1502
276