java--multithreading-is the static and non-static methods of the singleton mode thread-safe?

Source: Internet
Author: User

is the static and non-static methods of the singleton mode thread-safe?

 The answer is: whether the static and non-static methods of the singleton mode are thread-safe, regardless of the singleton mode. It is also said that if the static method or non-static method is not thread-safe, then it is not safe to use singleton mode for this class.

Gossip Hugh says, look at the code:
  

ImportJava.util.concurrent.ExecutorService;Importjava.util.concurrent.Executors; Public classTestsingleton { Public Static voidMain (string[] args)throwsException {executorservice pool= Executors.newfixedthreadpool (10);  for(intj = 0; J < 100000; J + +) {Pool.submit (NewThread () { Public voidrun () {Singleton.get (). Add ();        }            });        } pool.shutdownnow ();  while(!pool.isterminated ());    System.out.println (Singleton.get (). getcnt ()); }}classSingleton {Private StaticSingleton Singleton =NewSingleton (); intCNT = 0; PrivateSingleton () {} Public StaticSingleton Get () {returnSingleton; }     Public voidAdd () {CNT++; }     Public intgetcnt () {returnCNT; }}

The above results are generally not equal to 100000, and the number of runs.

Related notes:

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


strings is immutable (cannot being changed) so we ' re safe if the-the only Obje CT being "shared".

So many threads can is running the same method. They might not being running at the same time-it depends what many cores you had on your machine as the JVM maps Java Threa DS to OS threads, which is scheduled onto hardware threads. You therefore has little control over the these threads interleave without using complex synchronisation mechanisms.

Threads has their own stack so any method argument and local variable would be a unique for each thread.

Reference Links:

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

  

java--multithreading-is the static and non-static methods of the singleton mode thread-safe?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.