Java实现多线程的两种方式:
a) 同步块 b) 同步方法
public class TestMultiThread
{
public static void main(String [] args)
{
ImpThread it = new ImpThread();
new Thread(it).start();
new Thread(it).start();
new Thread(it).start();
}
}
class ImpThread implements Runnable
{
Object obj = new Object();
public void Run()
{
// the following code is synchronized block
while(TRUE)
{
synchronized(obj); // synchronized block
//do something for multithread
}
// the following code is synchronized method
method();
}
public synchronized void method()
{
while(TRUE)
{
//do something for multithread
}
}
}