java——多線程——單例模式的static方法和非static方法是否是安全執行緒的?

來源:互聯網
上載者:User

標籤:

  單例模式的static方法和非static方法是否是安全執行緒的?

  答案是:單例模式的static方法和非static方法是否是安全執行緒的,與單例模式無關。也就說,如果static方法或者非static方法不是安全執行緒的,那麼不會因為這個類使用了單例模式,而變的安全。

  閑話休說,看代碼:
  

import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class TestSingleton {    public static void main(String[] args) throws Exception {        ExecutorService pool = Executors.newFixedThreadPool(10);        for (int j = 0; j < 100000; j++) {            pool.submit(new Thread() {                public void run() {                    Singleton.get().add();                }            });        }        pool.shutdownNow();        while (!pool.isTerminated())            ;        System.out.println(Singleton.get().getcnt());    }}class Singleton {    private static Singleton singleton = new Singleton();    int cnt = 0;    private Singleton() {}    public static Singleton get() {        return singleton;    }    public void add() {        cnt++;    }    public int getcnt() {        return cnt;    }}

  上面的運行結果,一般是不會等於100000的,及運行次數。

  相關筆記:

  The heap is where all the objects live and the stacks are where the threads do their work. Each thread has its own stack and can‘t access each others stacks. Each thread also has a pointer into the code which points to the bit of code they‘re currently running. 

  When a thread starts running a new method it saves the arguments and local variables in that method on its own stack. Some of these values might be pointers to objects on the heap. If two threads are running the same method at the same time they will both have their code pointers pointing at that method and have their own copies of arguments and local variables on their stacks. They will only interfere with each other if the things on their stacks point to the same objects on the heap. In which case all sorts of things might happen.
Strings are immutable (cannot be changed) so we‘re safe if this is the only object being "shared".

  So many threads can be running the same method. They might not be running at the same time - it depends how many cores you have on your machine as the JVM maps Java threads to OS threads, which are scheduled onto hardware threads. You therefore have little control over the way these threads interleave without using complex synchronisation mechanisms.

  threads have their own stack so any method argument and local variable will be unique for each thread.

   

  參考連結:

  http://stackoverflow.com/questions/17343157/static-method-behavior-in-multi-threaded-environment-in-java

  

 

java——多線程——單例模式的static方法和非static方法是否是安全執行緒的?

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.