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 StuTh
评论0
最新资源