java的多线程同步初探
时间:2010-10-10 来源:tc310
主要的就是一个Thread类,Thread类拥有一个Run()方法,还有一个Thread.start()来启动线程开工。实现多线程有两个方法:
1.将要并行执行的操作封装到一个继承自Thread的类中去
代码 public class thread_ex extends Thread...{
public void init() ...{
}
public void start() ...{
//**will cll run() defualt.
}
public void stop() ...{
}
public void run()...{
//**do some parallelization works.
}
}
2.延伸Runnable接口,满足一些继承了其他类又想多线程的(因为java不像C++一样支持多继承)
public class ThreadTest extends FatherClass implements Runnable
public void init() ......{
}
public void start() ......{
//**will cll run() defualt.
}
public void stop() ......{
}
public void run()......{
//**do some parallelization works.
}
}
与线程有关的几个关键词是很重要的:synchronize wait notify notifyAll
如果不能很好的理解他们还是不要用的好。简单的介绍如下
synchronize 修饰定义一个对象或者方法或者属性的访问必须串行化访问,顾名思义像在银行取钱要排队一样,那个业务员就是一个synchronize 对象。只有像我等无钱之辈才去排队,那些个VIP自然不用了。
wait 和notify notifyAll是一家的,他们是对象级的锁,余天生驽钝,这个偶翻了许多资料才理解,如果不能理解就看代码吧。多线程中的一个去访问共享资源发现有人使用,于是就在这个资源上面wait,占用者如果使用完了就notify()一下通知下一个等待者可以进来了,notifyAll()就是说在外面等的一起上吧!可是只能服务与一个人,于是大家抢着进去,力气大的自然占便宜。可是在java世界里面JVM说了算。
有wait和notify的地方一定有synchronize,反过来不成立,wait 和notify不在Thread的势力范围
-----------------------------------------------------------
说正题,直奔代码!本例设计到三个类:ThreadController,PrintThread,Resource;也是顾名思义就不做解释。放代码:
代码 public class Resource...{
public static boolean inUse;
//public boolean inUse=false;
String name;
int sleepTime=0;
public synchronized void out(String n)...{
this.name=n;
while(Resource.inUse==true)
try...{
System.out.println("Name:"+getName()+" Waiting...");
this.wait();
}catch(InterruptedException ie)...{
ie.printStackTrace();
}
Resource.inUse=true; // entry the block and lock it
try...{
sleepTime=(int)(Math.random()*5000);
System.out.println("Name:"+getName()+" in use; define Sleep Time: "+sleepTime+",wait 50000 to exit the synchronize block");
Thread.sleep(5000);
System.out.println("Name:"+getName()+" end 5000 sleep . Sleep some time after notify to test if others can end first");
Thread.sleep(5000-sleepTime);
System.out.println("Name:"+getName()+" will Exit the synchronize block");
Resource.inUse=false;
System.out.println("Name:"+getName()+" End. "+Resource.inUse);
this.notifyAll();
}catch(InterruptedException ie)...{
ie.printStackTrace();
}
}
public String getName()...{
return name;
}
}
代码 public class ThreadController...{
public static void main(String args[])...{
PrintThread t1,t2,t3,t4;
Resource r=new Resource();
Resource.inUse=false;
t1 = new PrintThread("1",r);
t2 = new PrintThread("2",r);
t3 = new PrintThread("3",r);
t4 = new PrintThread("4",r);
t1.start();
t2.start();
t3.start();
t4.start();
System.out.println("Controller ended...");
//System.exit(0);
}
} 代码 public class PrintThread extends Thread...{
int sleepTime;
Resource rs;
public PrintThread(String id,Resource r)...{
super(id);
this.rs=r;
}
public void run()...{
try...{
System.out.println("Thread :"+getName()+" start...... wait to entry synchronize block");
rs.out(getName());
sleepTime=(int)(Math.random()*5000);
System.out.println("Thread :"+getName()+" start to sleep"+sleepTime+"mms");
sleep(sleepTime);
}catch(InterruptedException ie)...{
ie.printStackTrace();
}
System.out.println("Thread :"+getName()+" wakeup, end.");
}
}