日期:2014-05-20 浏览次数:20707 次
import org.junit.Test;
/**
* 同步线程测试
* @author jview
*
*/
public class testSynchronized {
@Test
public void testSynchronizedThread(){
System.out.println("-------testSynchronizedThread----");
Thread t1 = new Thread(new TheardTest("t1"));
t1.start();
Thread t2 = new Thread(new TheardTest("t2"));
t2.start();
System.out.println("-------testSynchronizedThread-end---");
}
}
class TheardTest implements Runnable{
public TheardTest(){
}
public TheardTest(String name){
this.name=name;
}
private String name;
public void run(){
if(name!=null)
System.out.println("----thread-name="+name);
SyncTest st = new SyncTest();
st.sleep4();
}
}
class SyncTest{
public synchronized void sleep4(){
try {
System.out.println("-------sleep4--");
Thread.sleep(4000);
System.out.println("-------sleep4-end-");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}