java——多线程——单例模式的static方法和非static方法是否是线程安全的?
单例模式的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中synchronized 用在实例方法和对象方法上面的区别 [问题分析]大家都知道,在Java中,synchronized 是用来表示同步的,我们可以synchronized ...
- synchronized 修饰在 static方法和非static方法的区别
Java中synchronized用在静态方法和非静态方法上面的区别 在Java中,synchronized是用来表示同步的,我们可以synchronized来修饰一个方法.也可以synchroniz ...
- static方法和非static方法的区别
●生命周期(Lifecycle):静态方法(Static Method)与静态成员变量一样,属于类本身,在类装载的时候被装载到内存(Memory),不自动进行销毁,会一直存在于内存中,直到JVM关闭. ...
- java static成员变量方法和非static成员变量方法的区别
这里的普通方法和成员变量是指,非静态方法和非静态成员变量首先static是静态的意思,是修饰符,可以被用来修饰变量或者方法. static成员变量有全局变量的作用 非static成员变量则 ...
- java static成员变量方法和非static成员变量方法的区别 ( 二 )
原创文章,未经作者允许,禁止转载!!! 静态成员变量不用new对象,在类加载的过程中就已经初始化存放在数据区域,静态成员变量是类和所有对象共有的,类和对象都可以改变它的值,每一次改变值之后,静态成员变 ...
- JAVA反射中的getFields()方法和getDeclaredFields ()方法的区别
JAVA反射中的getFields()方法和getDeclaredFields ()方法的区别 关于获取类的字段有两种方式:getFields()和getDeclaredFields().我们先来 ...
- java数组、java.lang.String、java.util.Arrays、java.lang.Object的toString()方法和equals()方法详解
public class Test { public static void main(String[] args) { int[] a = {1, 2, 4, 6}; int[] b = a; in ...
- Java8新特性(一)_interface中的static方法和default方法
什么要单独写个Java8新特性,一个原因是我目前所在的公司用的是jdk8,并且框架中用了大量的Java8的新特性,如上篇文章写到的stream方法进行过滤map集合.stream方法就是接口Colle ...
- DevExpress Winform使用单例运行程序方法和非DevExpress使用Mutex实现程序单实例运行且运行则激活窗体的方法
原文:DevExpress Winform使用单例运行程序方法和非DevExpress使用Mutex实现程序单实例运行且运行则激活窗体的方法 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA ...
随机推荐
- BZOJ-1923-外星千足虫-SDOI2010
描写叙述 分析 首先看上去这貌似是一个高斯消元的题目, 直觉吧- 每次给出的就相当于是一个方程. 然后非常easy想到n条虫子n个x, x_i的系数为0表示这个方程中没有i, 否则为1. 然后系数乘以 ...
- EC读书笔记系列之17:条款41、42、43、44、45、46
条款41 了解隐式接口与编译器多态 记住: ★classes和templates都支持接口和多态 ★对classes而言接口是显式的(explicit),以函数签名为中心.多态则是通过virtual函 ...
- Extjs Store 的用法详解
Ext.data.Store的基本用法 在使用之前,首先要创建一个Ext.data.Store的实例,如下面的代码所示. 每个store最少需要两个组件的支持,分别是proxy和reade ...
- jQuery获取Select选择的Text和Value(详细汇总)
语法解释: 1. $("#select_id").change(function(){//code...}); //为Select添加事件,当选择其中一项时触发 2. var ch ...
- javascript 常用函数
//获取元素的样式值. function getStyle(elem,name){ if(elem.style[name]){ return elem.style[name]; }else if(el ...
- jmeter实例演示
Jmeter 是比较轻便的性能测试工具,下面根据一个实例演示下jmeter的常见用法 一.前期准备,在使用之前,先分析测试需求,比如:需不要登录?需不需要监视服务器性能?多线程还是多循环?需不需根据流 ...
- A类型物料必须为装配拉式,供应子库为B仓
应用 Oracle Bill Of Materiel 层 Level Function 函数名 Funcgtion Name MT_BOMFDBOM 表单名 Form Name BOMFDBOM ...
- 贴片方式COB COF COG
英文简称: COB英文全称: Chip On Board中文全称: 通过邦定将IC裸片固定于印刷线路板上 英文简称: COF 英文全称: Chip On FPC 中文全称: 将IC固定于柔性线路板上 ...
- mini KMS Activator v1.3破解激活microsoft Office 2010 RTM
利用mini KMS Activator v1.3破解激活microsoft Office 2010 RTM方法,只是为体验office而做测试使用的哦...大家觉得好就自觉购买正版去... 使用步骤 ...
- flash跨域策略文件crossdomain.xml
flash在跨域时唯一的限制策略就是crossdomain.xml文件,该文件限制了flash是否可以跨域读写数据以及允许从什么地方跨域读写数据. 位于www.a.com域中的SWF文件要访问www. ...