java——多线程——单例模式的static方法和非static方法是否是线程安全的?
2015-04-30 21:44
489 查看
单例模式的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方法是否是线程安全的?
- Java多线程之Synchronized应用在static和非static方法上的区别
- Java static 静态方法 并发(是否线程安全)
- java 关于对“是否可以从一个static方法内部发出对非static方法的调用”问题的理解
- java学习之路 之 高级类特性2-关键字static、类变量、类方法、单例模式
- java多线程下synchronized修饰static方法与非static方法的区别
- 多线程中使用静态方法是否有线程安全问题
- 多线程中使用静态方法是否有线程安全问题
- static方法内部是否可以发出对非static方法的调用?
- Java中静态方法是否属于线程安全
- 在多线程中使用静态方法是否有线程安全问题
- Java基础问题:static方法中为什么不能使用非static的方法或变量
- 在多线程中使用静态方法是否有线程安全问题
- JAVA Static方法与单例模式的理解
- JAVA Static方法与单例模式的理解
- JAVA Static方法与单例模式的理解
- Java的static方法为什么不允许直接使用非static的变量
- Java中子类是否可以继承父类的static变量和方法而呈现多态特性
- 黑马程序员-------------多线程中的(线程、线程组、线程池、以及Java的设计模式)概念及方法的总结
- 面试题:是否可以从一个static方法内部发出对非static方法的调用?