设为首页 加入收藏

TOP

java synchronized (三)
2014-11-24 12:02:21 来源: 作者: 【 】 浏览:137
Tags:java synchronized
hronized in Method2(0)
Thread-1:synchronized in Method2(1)
Thread-1:synchronized in Method2(2)
Thread-1:synchronized in Method2(3)
Thread-1:synchronized in Method2(4)
main:synchronized in Method3(0)
main:synchronized in Method3(1)
main:synchronized in Method3(2)
main:synchronized in Method3(3)
main:synchronized in Method3(4)
[java]
package com.jialin;
import java.util.concurrent.TimeUnit;
public class Test2 extends AbstractTest {
public synchronized void Method1() {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName()
+ ":synchronized in Method1("+i+")");
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void Method2() {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName()
+ ":no synchronized in Method2("+i+")");
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void Method3() {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName()
+ ":no synchronized in Method3("+i+")");
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
// 初始化Test1对象
final AbstractTest ts = new Test2();
// 线程1
Thread1 thread1 = new Thread1(ts);
thread1.start();
// 线程2
Thread2 thread2 = new Thread2(ts);
thread2.start();
//线程3
Thread3 thread3=new Thread3(ts);
thread3.start();
// main线程
ts.Method1();
}
}
package com.jialin;
import java.util.concurrent.TimeUnit;
public class Test2 extends AbstractTest {
public synchronized void Method1() {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName()
+ ":synchronized in Method1("+i+")");
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void Method2() {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName()
+ ":no synchronized in Method2("+i+")");
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void Method3() {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName()
+ ":no synchronized in Method3("+i+")");
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
// 初始化Test1对象
final AbstractTest ts = new Test2();
// 线程1
Thread1 thread1 = new Thread1(ts);
thread1.start();
// 线程2
Thread2 thread2 = new Thread2(ts);
thread2.start();
//线程3
Thread3 thread3=new Thread3(ts);
thread3.start();
// main线程
ts.Method1();
}
}
运行结果:
首页 上一页 1 2 3 4 5 下一页 尾页 3/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇获取系统中所有应用的方法 下一篇实现通用的PreparedStatement插入..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: