public class SynchronizedTest1 extends Thread
{
private synchronized void testSynchronizedMethod()
{
for (int i = 0; i < 10; i++)
{
System.out.println(Thread.currentThread().getName()
+ " testSynchronizedMethod:" + i);
try
{
Thread.sleep(100);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
@Override
public void run()
{
testSynchronizedMethod();
}
public static void main(String[] args)
{
SynchronizedTest1 t = new SynchronizedTest1();
t.start();
t.testSynchronizedMethod();
}
}
main testSynchronizedMethod:0 main testSynchronizedMethod:1 main testSynchronizedMethod:2 main testSynchronizedMethod:3 main testSynchronizedMethod:4 main testSynchronizedMethod:5 main testSynchronizedMethod:6 main testSynchronizedMethod:7 main testSynchronizedMethod:8 main testSynchronizedMethod:9 Thread-0 testSynchronizedMethod:0 Thread-0 testSynchronizedMethod:1 Thread-0 testSynchronizedMethod:2 Thread-0 testSynchronizedMethod:3 Thread-0 testSynchronizedMethod:4 Thread-0 testSynchronizedMethod:5 Thread-0 testSynchronizedMethod:6 Thread-0 testSynchronizedMethod:7 Thread-0 testSynchronizedMethod:8 Thread-0 testSynchronizedMethod:9
public static void main(String[] args)
{
Thread t = new SynchronizedTest1();
t.start();
Thread t1 = new SynchronizedTest1();
t1.start();
}
Thread-0 testSynchronizedMethod:0 Thread-1 testSynchronizedMethod:0 Thread-0 testSynchronizedMethod:1 Thread-1 testSynchronizedMethod:1 Thread-0 testSynchronizedMethod:2 Thread-1 testSynchronizedMethod:2 Thread-0 testSynchronizedMethod:3 Thread-1 testSynchronizedMethod:3 Thread-0 testSynchronizedMethod:4 Thread-1 testSynchronizedMethod:4 Thread-0 testSynchronizedMethod:5 Thread-1 testSynchronizedMethod:5 Thread-0 testSynchronizedMethod:6 Thread-1 testSynchronizedMethod:6 Thread-0 testSynchronizedMethod:7 Thread-1 testSynchronizedMethod:7 Thread-0 testSynchronizedMethod:8 Thread-1 testSynchronizedMethod:8 Thread-0 testSynchronizedMethod:9 Thread-1 testSynchronizedMethod:9
public class SynchronizedTest1 extends Thread
{
private static synchronized void testSynchronizedMethod()
{
for (int i = 0; i < 10; i++)
{
System.out.println(Thread.currentThread().getName()
+ " testSynchronizedMethod:" + i);
try
{
Thread.sleep(100);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
@Override
public void run()
{
testSynchronizedMethod();
}
public static void main(String[] args)
{
Thread t = new SynchronizedTest1();
t.start();
Thread t1 = new SynchronizedTest1();
t1.start();
}
}
Thread-0 testSynchronizedMethod:0 Thread-0 testSynchronizedMethod:1 Thread-0 testSynchronizedMethod:2 Thread-0 testSynchronizedMethod:3 Thread-0 testSynchronizedMethod:4 Thread-0 testSynchronizedMethod:5 Thread-0 testSynchronizedMethod:6 Thread-0 testSynchronizedMethod:7 Thread-0 testSynchronizedMethod:8 Thread-0 testSynchronizedMethod:9 Thread-1 testSynchronizedMethod:0 Thread-1 testSynchronizedMethod:1 Thread-1 testSynchronizedMethod:2 Thread-1 testSynchronizedMethod:3 Thread-1 testSynchronizedMethod:4 Thread-1 testSynchronizedMethod:5 Thread-1 testSynchronizedMethod:6 Thread-1 testSynchronizedMethod:7 Thread-1 testSynchronizedMethod:8 Thread-1 testSynchronizedMethod:9
public class SynchronizedTest2 extends Thread
{
private void testSynchronizedBlock()
{
synchronized (this)
{
for (int i = 0; i < 10; i++)
{
System.out.println(Thread.currentThread().getName()
+ " testSynchronizedBlock:" + i);
try
{
Thread.sleep(100);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
@Override
public void run()
{
testSynchronizedBlock();
}
public static void main(String[] args)
{
SynchronizedTest2 t = new SynchronizedTest2();
t.start();
t.testSynchronizedBlock();
}
}
main testSynchronizedBlock:0 main testSynchronizedBlock:1 main testSynchronizedBlock:2 main testSynchronizedBlock:3 main testSynchronizedBlock:4 main testSynchronizedBlock:5 main testSynchronizedBlock:6 main testSynchronizedBlock:7 main testSynchronizedBlock:8 main testSynchronizedBlock:9 Thread-0 testSynchronizedBlock:0 Thread-0 testSynchronizedBlock:1 Thread-0 testSynchronizedBlock:2 Thread-0 testSynchronizedBlock:3 Thread-0 testSynchronizedBlock:4 Thread-0 testSynchronizedBlock:5 Thread-0 testSynchronizedBlock:6 Thread-0 testSynchronizedBlock:7 Thread-0 testSynchronizedBlock:8 Thread-0 testSynchronizedBlock:9
public class SynchronizedTest2 extends Thread
{
private void testSynchronizedBlock()
{
synchronized (SynchronizedTest2.class)
{
for (int i = 0; i < 10; i++)
{
System.out.println(Thread.currentThread().getName()
+ " testSynchronizedBlock:" + i);
try
{
Thread.sleep(100);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
@Override
public void run()
{
testSynchronizedBlock();
}
public static void main(String[] args)
{
Thread t = new SynchronizedTest2();
t.start();
Thread t2 = new SynchronizedTest2();
t2.start();
}
}
Thread-0 testSynchronizedBlock:0 Thread-0 testSynchronizedBlock:1 Thread-0 testSynchronizedBlock:2 Thread-0 testSynchronizedBlock:3 Thread-0 testSynchronizedBlock:4 Thread-0 testSynchronizedBlock:5 Thread-0 testSynchronizedBlock:6 Thread-0 testSynchronizedBlock:7 Thread-0 testSynchronizedBlock:8 Thread-0 testSynchronizedBlock:9 Thread-1 testSynchronizedBlock:0 Thread-1 testSynchronizedBlock:1 Thread-1 testSynchronizedBlock:2 Thread-1 testSynchronizedBlock:3 Thread-1 testSynchronizedBlock:4 Thread-1 testSynchronizedBlock:5 Thread-1 testSynchronizedBlock:6 Thread-1 testSynchronizedBlock:7 Thread-1 testSynchronizedBlock:8 Thread-1 testSynchronizedBlock:9
public class TwoThread {
public static void main(String[] args) {
final TwoThread twoThread = new TwoThread();
Thread t1 = new Thread(new Runnable() {
public void run() {
twoThread.syncMethod();
}
}, "A");
Thread t2 = new Thread(new Runnable() {
public void run() {
twoThread.syncMethod();
}
}, "B");
t1.start();
t2.start();
}
public synchronized void syncMethod() {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + " : " + i);
try {
Thread.sleep(500);
} catch (InterruptedException ie) {
}
}
}
}
A : 0 A : 1 A : 2 A : 3 A : 4 B : 0 B : 1 B : 2 B : 3 B : 4
public class TwoObject {
public static void main(String[] args) {
final TwoObject object1 = new TwoObject();
Thread t1 = new Thread(new Runnable() {
public void run() {
object1.syncMethod();
}
}, "Object1");
t1.start();
final TwoObject object2 = new TwoObject();
Thread t2 = new Thread(new Runnable() {
public void run() {
object2.syncMethod();
}
}, "Object2");
t2.start();
}
public synchronized void syncMethod() {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + " : " + i);
try {
Thread.sleep(500);
} catch (InterruptedException ie) {
}
}
}
}
Object2 : 0 Object1 : 0 Object1 : 1 Object2 : 1 Object2 : 2 Object1 : 2 Object2 : 3 Object1 : 3 Object1 : 4 Object2 : 4
public class SyncAndNoSync {
public static void main(String[] args) {
final SyncAndNoSync syncAndNoSync = new SyncAndNoSync();
Thread t1 = new Thread(new Runnable() {
public void run() {
syncAndNoSync.syncMethod();
}
}, "A");
t1.start();
Thread t2 = new Thread(new Runnable() {
public void run() {
syncAndNoSync.noSyncMethod();
}
}, "B");
t2.start();
}
public synchronized void syncMethod() {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + " at syncMethod(): " + i);
try {
Thread.sleep(500);
} catch (InterruptedException ie) {
}
}
}
public void noSyncMethod() {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + " at noSyncMethod(): " + i);
try {
Thread.sleep(500);
} catch (InterruptedException ie) {
}
}
}
}
B at noSyncMethod(): 0 A at syncMethod(): 0 B at noSyncMethod(): 1 A at syncMethod(): 1 B at noSyncMethod(): 2 A at syncMethod(): 2 B at noSyncMethod(): 3 A at syncMethod(): 3 A at syncMethod(): 4 B at noSyncMethod(): 4
public class TwoSyncMethod {
public static void main(String[] args) {
final TwoSyncMethod twoSyncMethod = new TwoSyncMethod();
Thread t1 = new Thread(new Runnable() {
public void run() {
twoSyncMethod.syncMethod1();
}
}, "A");
t1.start();
Thread t2 = new Thread(new Runnable() {
public void run() {
twoSyncMethod.syncMethod2();
}
}, "B");
t2.start();
}
public synchronized void syncMethod1() {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + " at syncMethod1(): " + i);
try {
Thread.sleep(500);
} catch (InterruptedException ie) {
}
}
}
public synchronized void syncMethod2() {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + " at syncMethod2(): " + i);
try {
Thread.sleep(500);
} catch (InterruptedException ie) {
}
}
}
}
A at syncMethod1(): 0 A at syncMethod1(): 1 A at syncMethod1(): 2 A at syncMethod1(): 3 A at syncMethod1(): 4 B at syncMethod2(): 0 B at syncMethod2(): 1 B at syncMethod2(): 2 B at syncMethod2(): 3 B at syncMethod2(): 4
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有