import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class TestMain {
static volatile int count = 0;
Lock lock = new ReentrantLock();
Object obj = new Object();
public static void main(String[] str){
for(int i=0;i<1000;i++){
TestMain tm = new TestMain();
Stuff stuff = tm.new Stuff();
stuff.setName("thread:"+i);
stuff.start();
}
while(Thread.activeCount()>1)Thread.yield();
System.out.print(count);
}
class Stuff extends Thread{
public Stuff(){};
@Override
public void run(){
//Lock lock = new ReentrantLock();
for(int i=0;i<100;i++){
synchronized(obj){
count++;
}
}
}
}
我不是对变量加了锁了吗?为何不行呢?但是下面的写法却可以?
public int inc = 0;
Lock lock =new ReentrantLock();
public void increase(){
lock.lock();
try{
inc++;
}finally{
lock.unlock();
}
}
public static void main(String[] args){
final TestMain2 test = new TestMain2();
for(int i=0;i<100;i++){
new Thread(){
public void run(){
for(int j = 0;j<1000;j++)test.increase();
}
}.start();
}
while(Thread.activeCount()>1)Thread.yield();
System.out.println(test.inc);
}
你这个有主要是持有数的对象变了,上面的TestMain 的对象你创建了多个,所持有的数势必会产生多个,下面的TestMain 对象创建了一个,保证了数的唯一,所以下面可以的。
** 我简化了一下你上面的代码,然后不知道是不是我比较捞. Stuff stuff=tm.new Stuff();我还是没看懂**
public class Stuff implements Runnable{
Object obj=new Object();
static int count=0;
@Override
public void run() {
for(int i=0;i<100;i++) {
synchronized (obj) {
count++;
System.out.println(count);
}
}
}
}
public class TestMain2 {
public static void main(String[] args) {
for(int i=0;i<1000;i++) {
Stuff stuff=new Stuff();
new Thread(stuff).start();
while(Thread.activeCount()>1)
Thread.yield();
}
}
}
至于你下面的代码不,不如写成这样
public class TestMain {
static int count = 0;
static Object obj = new Object();
public static void main(String[] args) {
for (int i = 0; i < 1000; i++) {
new Thread() {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
synchronized (obj) {
count++;
System.out.println(count);
}
}
}
}.start();
while (Thread.activeCount() > 1)
Thread.yield();
}
}
}
这是因为:
第一段代码,使用synchronized锁住的对象obj是不同的对象,因为obj是你TestMain的成员变量,你new了不同的TestMain对象,
所以加锁的对象不是同一个,没有起到加锁同步的作用
而第二段代码,只new了一个TestMain对象,加锁的是用一个lock,所以起到了同步作用
锁住Stuff这个类,只有锁住这个类才能保证同步