您的位置:首页 > 其它

【多线程】synchronized同步方法

2012-07-30 16:21 411 查看
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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐