public class ThreadTest
{
public static void main(String[] args)
{
Example example = new Example();
Thread t1 = new Thread1(example);
Thread t2 = new Thread1(example);
t1.start();
t2.start();
}
}
class Example
{
public synchronized void execute()
{
for (int i = 0; i < 10; ++i)
{
try
{
Thread.sleep(500);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
System.out.println("Hello: " + i);
}
}
}
class Thread1 extends Thread
{
private Example example;
public Thread1(Example example)
{
this.example = example;
}
@Override
public void run()
{
example.execute();
}
}
public class ThreadTest
{
public static void main(String[] args)
{
Example example = new Example();
Thread t1 = new Thread1(example);
Thread t2 = new Thread2(example);
t1.start();
t2.start();
}
}
class Example
{
public synchronized void execute()
{
for (int i = 0; i < 20; ++i)
{
try
{
Thread.sleep((long) Math.random() * 1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
System.out.println("Hello: " + i);
}
}
public synchronized void execute2()
{
for (int i = 0; i < 20; ++i)
{
try
{
Thread.sleep((long) Math.random() * 1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
System.out.println("World: " + i);
}
}
}
class Thread1 extends Thread
{
private Example example;
public Thread1(Example example)
{
this.example = example;
}
@Override
public void run()
{
example.execute();
}
}
class Thread2 extends Thread
{
private Example example;
public Thread2(Example example)
{
this.example = example;
}
@Override
public void run()
{
example.execute2();
}
}
public class ThreadTest
{
public static void main(String[] args)
{
Example example = new Example();
Thread t1 = new Thread1(example);
// 此处即便传入不同的对象,静态方法同步仍然不允许多个线程同时执行
example = new Example();
Thread t2 = new Thread2(example);
t1.start();
t2.start();
}
}
class Example
{
public synchronized static void execute()
{
for (int i = 0; i < 20; ++i)
{
try
{
Thread.sleep((long) Math.random() * 1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
System.out.println("Hello: " + i);
}
}
public synchronized static void execute2()
{
for (int i = 0; i < 20; ++i)
{
try
{
Thread.sleep((long) Math.random() * 1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
System.out.println("World: " + i);
}
}
}
class Thread1 extends Thread
{
private Example example;
public Thread1(Example example)
{
this.example = example;
}
@Override
public void run()
{
Example.execute();
}
}
class Thread2 extends Thread
{
private Example example;
public Thread2(Example example)
{
this.example = example;
}
@Override
public void run()
{
Example.execute2();
}
}
synchronized(object)
{
}
public class ThreadTest
{
public static void main(String[] args)
{
Example example = new Example();
Thread t1 = new Thread1(example);
Thread t2 = new Thread2(example);
t1.start();
t2.start();
}
}
class Example
{
private Object object = new Object();
public void execute()
{
synchronized (object)
{
for (int i = 0; i < 20; ++i)
{
try
{
Thread.sleep((long) Math.random() * 1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
System.out.println("Hello: " + i);
}
}
}
public void execute2()
{
synchronized (object)
{
for (int i = 0; i < 20; ++i)
{
try
{
Thread.sleep((long) Math.random() * 1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
System.out.println("World: " + i);
}
}
}
}
class Thread1 extends Thread
{
private Example example;
public Thread1(Example example)
{
this.example = example;
}
@Override
public void run()
{
example.execute();
}
}
class Thread2 extends Thread
{
private Example example;
public Thread2(Example example)
{
this.example = example;
}
@Override
public void run()
{
example.execute2();
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有