public class ThreadB extends Thread {
int total;
@Override
public void run() {
synchronized (this) {
for(int i = 0 ; i < 10 ; i++){
total += i;
}
notify();
//System.out.println(total);
}
}
}class ThreadA{
public static void main(String[] args){
ThreadB b = new ThreadB();
b.start();
try{
Thread.sleep(1000); }catch(Exception e){
e.printStackTrace();
}
//synchronized (Thread.currentThread()) {
synchronized (b) {
System.out.println("aaa");
try{
//Thread.currentThread().wait();
b.wait();
}catch(Exception e){
e.printStackTrace();
}
}
System.out.println(b.total);
}
}
int total;
@Override
public void run() {
synchronized (this) {
for(int i = 0 ; i < 10 ; i++){
total += i;
}
notify();
//System.out.println(total);
}
}
}class ThreadA{
public static void main(String[] args){
ThreadB b = new ThreadB();
b.start();
try{
Thread.sleep(1000); }catch(Exception e){
e.printStackTrace();
}
//synchronized (Thread.currentThread()) {
synchronized (b) {
System.out.println("aaa");
try{
//Thread.currentThread().wait();
b.wait();
}catch(Exception e){
e.printStackTrace();
}
}
System.out.println(b.total);
}
}
main线程能进入到synchronized块中么?
那么synchronized块到底是在做什么呢?
main线程能进入到synchronized块中么?
那么synchronized块到底是在做什么呢?不是刚进入synchronized块就阻止了main线程了么?
为什么还要打出aaa呢?