????1.?????з????????
????synchronized????????????????????????????????????synchronized????????????????synchronized???????????????????
??????????

 

public class ThreadTest {
public static void main(String[] args) {
Stu stu = new Stu();
StuThread1 t1 = new StuThread1(stu);
t1.start();
StuThread2 t2 = new StuThread2(stu);
t2.start();
}
}
class StuThread1 extends Thread {
Stu stu;
public StuThread1(Stu stu) {
this.stu = stu;
}
public void run() {
stu.read1();
}
}
class StuThread2 extends Thread {
Stu stu;
public StuThread2(Stu stu) {
this.stu = stu;
}
public void run() {
stu.read2();
}
}
class Stu {
public synchronized void read1() {
System.out.println("read1 begin");
try {
Thread.currentThread().sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("read1 end");
}
public synchronized void read2() {
System.out.println("read2 begin");
try {
Thread.currentThread().sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("read2 end");
}
}