没太看懂楼主的意思。 你的意思是不是说多线程中考虑线程同步的问题? 如果是的话,那么你需要使用同步机制。同步机制的实现有两种,其中一种是直接使用synchronized关键字。举个例子public class Test{ private static int count = 0;
public synchronized void inc(){ count ++; } }这样就不会冲突。
public class Test { private static boolean inUse; public Test() { if (inUse) { throw new IllegalStateException(); } inUse = true; try { Thread.sleep(100); } catch (InterruptedException ex) { ex.printStackTrace(); } inUse = false; } public static void main(String[] args) { for (int i = 0; i < 10; i++) { new Thread() { public void run() { for (;;) { new Test(); } } }.start(); } } }
即使不sleep,结果也是一样,构造函数又不是static段。 public class Test { private static boolean inUse; public Test() { if (inUse) { throw new IllegalStateException(); } inUse = true; inUse = false; } public static void main(String[] args) { for (int i = 0; i < 10; i++) { new Thread() { public void run() { for (;;) { new Test(); } } }.start(); } } }
public class Person{
public static int count = 0;
public Person(){
count++;//这种使用方式不会有问题,因为不可能同时存在多个线程同时执行构造函数
}
}
为什么不可能,
同时多个线程一起new一个对象不可能吗?
public static int count;
public static countAdd{
++count;
}
}
public static int count;
public static void countAdd(){
++count;
}
}
对变量的写操作不依赖于当前值。
该变量没有包含在具有其他变量的不变式中。
那么可以用 Volatile 修饰这个变量 。
Volatile 变量具有 synchronized 的可见性特性,但是不具备原子特性。这就是说线程能够自动发现 volatile 变量的最新值。Volatile 变量可用于提供线程安全,但是只能应用于非常有限的一组用例:多个变量之间或者某个变量的当前值与修改后值之间没有约束。因此,单独使用 volatile 还不足以实现计数器、互斥锁或任何具有与多个变量相关的不变式(Invariants)的类
你的意思是不是说多线程中考虑线程同步的问题?
如果是的话,那么你需要使用同步机制。同步机制的实现有两种,其中一种是直接使用synchronized关键字。举个例子public class Test{
private static int count = 0;
public synchronized void inc(){
count ++;
}
}这样就不会冲突。
public class Test { private static boolean inUse; public Test() {
if (inUse) {
throw new IllegalStateException();
}
inUse = true;
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
inUse = false;
} public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
new Thread() {
public void run() {
for (;;) {
new Test();
}
}
}.start();
}
}
}
public class Test { private static boolean inUse; public Test() {
if (inUse) {
throw new IllegalStateException();
}
inUse = true;
inUse = false;
} public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
new Thread() {
public void run() {
for (;;) {
new Test();
}
}
}.start();
}
}
}
//允许访问控制的代码
}这段代码求大牛详解下呗放在括号里的那个对象是干什么用的呢?
如果问为何int的count++会有线程安全性问题,那是因为,JLS只规定了单个int/float的读or写是原子的,但是由于count++涉及到读+写,因此多线程下不安全。
int a=3;
synchronized(a){
a.wait();
}
对于基本类型没有方法wait,nodify,这样答还比较好点。
对于锁引用和锁基本类型实质都是在栈中。
或者使用AtomicInteger
public static synchronized void add(){
count++;
}如果不是单例就private static int count;
private static Object lock = new Object();
public static void add(){
synchronized(lock)
{
count++;
}
}
private static AtomicInteger count = new AtomicInteger(0);
public Test(){
count.incrementAndGet();
}
}
这样的话就不用锁了,用JAVA的原子就不会发生并发的情况了,最好不要用锁
不会用啊
和Integer一样用吗?老师没有教过,要自己查!
我印象中对于静态方法是使用synchronized 即使不是单例,锁照样有效,如果把你写的方法去掉static关键字的话就和你说的一样吧。
我的压缩包里面不止一两样东西哦,那可是我学习和工作的所有资料,不管是学习还是工作都会需要。
http://download.csdn.net/user/yangtonghai
i++;
}试试这个。 我就这样干的。
package test;import java.util.concurrent.atomic.AtomicInteger;public class MainClass {}class c{
static AtomicInteger count = new AtomicInteger();
int number;
c(){
number = count.getAndIncrement();
}
}代码是想这样的么?