该程序的结果为什么会是
2000
1000
1000
我的理解是这样的!不管线程1还是线程2优先执行,是不是主线程都能优先输出System.out.println(tt.b);
这样应该能看到100的值。但是为什么每次都是这个结果?sleep不是不释放锁吗?谁可以给个详细的解释?谢谢!public class TT implements Runnable{
int b = 100; public synchronized void run(){
try {
modify_1();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public synchronized void modify_1() throws InterruptedException{
b=1000;
Thread.sleep(5000);
System.out.println(b);
}
public synchronized void modify_2() throws InterruptedException{
Thread.sleep(2500);
b=2000;
System.out.println(b);
}
public static void main(String[] args) throws InterruptedException {
TT tt = new TT();
Thread th = new Thread(tt);
th.start();
tt.modify_2();
System.out.println(tt.b);
}
}
2000
1000
1000
我的理解是这样的!不管线程1还是线程2优先执行,是不是主线程都能优先输出System.out.println(tt.b);
这样应该能看到100的值。但是为什么每次都是这个结果?sleep不是不释放锁吗?谁可以给个详细的解释?谢谢!public class TT implements Runnable{
int b = 100; public synchronized void run(){
try {
modify_1();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public synchronized void modify_1() throws InterruptedException{
b=1000;
Thread.sleep(5000);
System.out.println(b);
}
public synchronized void modify_2() throws InterruptedException{
Thread.sleep(2500);
b=2000;
System.out.println(b);
}
public static void main(String[] args) throws InterruptedException {
TT tt = new TT();
Thread th = new Thread(tt);
th.start();
tt.modify_2();
System.out.println(tt.b);
}
}
th.start();
时候。由于线程的启动需要时间。
在启动的这个时间内。tt.modify_2();
会被执行。 可是.modify_2 中 Thread.sleep(2500); 这时的 b 还是 100
要睡 2500 秒。由于使用了同步所以其他线程也只有等待,直到主线程把 modify_2
执行完。输出 2000 . 这时 th已经启动 并立刻把 的值修改为 b=1000; 并去睡。
主线程会在这个时候 执行 System.out.println(tt.b); 输出 1000;
等 modify_1() 睡醒后 再输出 1000;所以中间那个 1000 是 System.out.println(tt.b); 输出 的。
最后哪个 1000 是 modify_1() 输出的。
又只有 10 分 呵呵!
static int c = 100;
int b=100; public synchronized void run(){
try {
modify_1();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public synchronized void modify_1() throws InterruptedException{
b=1000;
Thread.sleep(5000);
System.out.println(b);
}
public synchronized void modify_2() throws InterruptedException{
Thread.sleep(2500);
b=2000;
System.out.println(b);
}
public static void main(String[] args) throws InterruptedException {
Test tt = new Test();
Thread th = new Thread(tt);
th.start();
tt.modify_2();
System.out.println(Test.c);
}
}这个问题 我也不懂,我也不知道如何调用全局变量B,那个B好像被下面的B给重新定义了,反正我上面的代码可以打印出100
th启动后,就run,再执行 modify_1();因为引入了同步,但在睡上5秒中,tt去执行modify_1();应该需要等th释放出锁的啊,应该没法执行才对啊
我认为结果是 1000,1000,2000.
望赐教!
当解锁后th线程从阻塞状态转为就绪状态,系统为其分配时间片,但并未执行
执行的依然是从2500中恢复的main线程
这个程序的正确输出是:2000,2000,1000。
main启动线程,线程run调用modify_1(),线程sleep5秒。tt线程虽然sleep
但是并不影响main线程继续执行,因此main线程调用tt.modify_2(); 打印2000;
main线程调用tt.modify_2();后,tt线程的sleep还没有结束,main线程继续执行
System.out.println(tt.b);打印2000;main线程执行完了,但是tt线程还没有执行完,
所以main线程不会退出,等待tt线程执行完打印1000后,main线程退出,程序结束。
我的机器每次运行都是2000,1000,1000
虽然一楼的说法可以讲通,但是为什么不会出现2000,2000,1000
因为modify_2执行完,主线程还是可以继续执行System.out.println(tt.b);这句话的。这是b=2000,
最后modify_1方法执行,才出1000.
推理上应该是2种结果
2000,2000,1000
2000,1000,1000
不知道大家的测试结果都是什么?