我想试一下synchronized关键字是不是有效?所以写了一个很简单的类,但是就是不能调到数字按序排列。不知道错在哪里了。
数字序列类:
[code="java"]
package com.testthread;
public class UnsafeSequence {
private int value = 0;
public synchronized int getValue(){
value = value+1;
return value;
}
}
[/code]
线程类
[code="java"]
package com.testthread;
public class TestThread extends Thread {
private UnsafeSequence us;
public TestThread(UnsafeSequence us, String threadname) {
super(threadname);
this.us = us;
}
@Override
public void run() {
String classname = this.getClass().getSimpleName();
String threadname = currentThread().getName();
for (int i = 0; i < 5; i++) {
System.out.println(classname + "[" + threadname + "]:"
+ us.getValue());
}
}
}
[/code]
Main类
[code="java"]
package com.testthread;
public class MainClass {
public static void main(String[] args) throws InterruptedException {
System.out.println("Main started");
UnsafeSequence us = new UnsafeSequence();
TestThread at = new TestThread(us,"at");
TestThread bt = new TestThread(us,"bt");
at.start();
bt.start();
System.out.println("Main ended");
}
}
[/code]
可是结果是:
[code="java"]
Main started
Main ended
TestThread[bt]:2
TestThread[bt]:3
TestThread[at]:1
TestThread[at]:5
TestThread[at]:6
TestThread[at]:7
TestThread[at]:8
TestThread[bt]:4
TestThread[bt]:9
TestThread[bt]:10
[/code]
我想让数字按序排列,可是数字没有按序排列,请问哪里写错了,谢谢
你真要输出1-10的话,应该在UnsafeSequence.getValue里面输出value的值,肯定就是1-10了,因为System.out.println的执行和getValue的执行是两个方法的调用,getValue方法调用返回以后,不一定它就马上执行了System.out.println,因为这个时候可能又转到别的线程执行去了,加synchronized唯一能保证的是,getValue无论多少个线程同时调用,他一定会序列化执行,也就是在这里输出value的值,一定是递增的。
概念混淆了吧,synchronized只能保证同一时刻共享资源被唯一访问,可没说能排序啊
[code="java"]
public class UnsafeSequence {
private AtomicInteger value = new AtomicInteger(0);
public int getValue() {
return value.incrementAndGet();
}
public static void main(String[] args) {
System.out.println("Main started");
UnsafeSequence us = new UnsafeSequence();
TestThread at = new TestThread(us, "at");
TestThread bt = new TestThread(us, "bt");
at.start();
bt.start();
System.out.println("Main ended");
}
}
class TestThread extends Thread {
private UnsafeSequence us;
public TestThread(UnsafeSequence us, String threadname) {
super(threadname);
this.us = us;
}
@Override
public void run() {
String classname = this.getClass().getSimpleName();
String threadname = currentThread().getName();
for (int i = 0; i < 10; i++) {
synchronized (us) {
System.out.println(classname + "[" + threadname + "]:" + us.getValue());
}
}
}
}
[/code]
你的问题是这样的 楼上的已经说的很清楚了 我在简单说一下
若多个线程 同时跑到
System.out.println(classname + "[" + threadname + "]:"
+ us.getValue());
这句话 这时 方法getValue()会有锁 所以只能一个线程进去 但是 并不能保证谁先输出结果 所以顺序会乱
修改:
1.去掉 getValue()方法前的 Synchronized
2.synchronized (us) {
System.out.println(classname + "[" + threadname + "]:"
+ us.getValue());
}
(在输出那句话上 用synchronized锁定 这样保证 一个一个的进入)