public class SynchronizedTest2 {
public static void main(String[] args) {
ThreadTest t1 = new ThreadTest();
new Thread(t1).start();
new Thread(t1).start();
System.out.println(t1.call());
}
}
class ThreadTest implements Runnable{
private int x;
private int y;
@Override
public synchronized void run() {
for(int i=0;i<4;i++){
x++;
y++;
try {
Thread.sleep(200);
} catch (InterruptedException e) {
System.out.println("线程出错了!!!");
}
System.out.println(Thread.currentThread().getName() + " x==" + x + ",y==" + y + " " + i);
}
}
public synchronized String call(){
String name = Thread.currentThread().getName();
return "hellow " + name;
}
}
【多线程】synchronized同步方法
最新推荐文章于 2023-02-19 07:15:13 发布