import java.lang.Thread;
/**
 * @author w2
 * 2005/07/25
 * <p>多线程的学习用例
 * 本程序分析模拟多线程同时调用同一个消耗时间的操作时采用(同步/static方法/多实例访问)三种实现方式的差异
 * 在类<code>thd(n)</code>中选择注释的代码实现相应的调用方式.
 * 
 * <li>Thread的执行是无序的,也即跟具体哪个Thread.start()的顺序无关.除非使用setPriority设置优先
 * <li>synchronized同步的原理是对于一个具体的对象(指定的一小块内存)加锁,所以对于多线程在访问<b>一个对象</b>
 * 时,如果其中有共享变量,加synchronized才能实现共享变量的同步
 * <p>本程序实现的是从现象上区分synchronized与static方法的区别,显示同步是多线程是依次执行,而多线程调用
 * static方法则是并发执行,另外还有一种情况是对每次执行方法都产生个单独的实例然后执行.
 * 通过分析程序执行时间产生如下结论:<br>
 * <b>
 * <li>对于单一线程执行对象方法,消耗时间看作执行的最短单位时间
 * <li>对于调用单一synchronized对象的方法,多线程依次执行,总消耗时间大约为n个单位时间的相加和
 * <li>对于调用单一static方法,多线程同时并发执行,但总消耗时间也近似为n*单位时间,不同的是多个线程中每个线程都
 * 会消耗总消耗时间来完成操作
 * <li>对于产生多个实例来调用此方法,多线程执行跟static方法类似,接近与同时执行同时结束
 * 以上分析发现对于多线程调用一个耗时的操作,无论使用synchronized还是static还是new对象再执行消耗的时间是差不
 * 多的,但在内存使用上使用第三种的占用率会高,而且第一种因为尽管慢但对单一线程来说算是最快的响应了(还不敢确定)
 * 综合来看反而synchronized的效率最高!?(当然最好的办法是解决操作执行速度慢这个瓶颈)
 * </b>
 */
public class TestThread {

private static int x=0;
public  void sync (int s,String op){ synchronized(this){
/*
 *  在这里如果使用了wait挂起该线程则会释放锁,另一个线程会进来,然后也挂起...最后会很快的依次唤醒-执行
 *  try{
wait(1000);
}catch(Exception e){
e.printStackTrace();
}
*/

/*
 * 这里是执行一个大操作延迟一下方法执行时间
 * */
long startTime = System.currentTimeMillis();
System.out.println(op+"startTime:"+startTime);
int result = 0 ;
for(int i=0;i<1000000000;i++)
result = i;
long stopTime = System.currentTimeMillis();
System.out.println(op+"stopTime:"+stopTime+".last:"+(stopTime-startTime)+"");

System.out.println("op:"+op);
if(op.indexOf("1")>0)
x += s;
if(op.indexOf("2")>0)
x -= s;
if(op.indexOf("3")>0)
x *= s;

System.out.println("x="+x);
} }

public static void stati(String s){

long startTime = System.currentTimeMillis();
/*
 * 这里是执行一个大操作延迟一下方法执行时间
 * */
System.out.println(s+"startTime:"+startTime);
int result = 0 ;
for(int i=0;i<1000000000;i++)
result = i;
long stopTime = System.currentTimeMillis();
System.out.println(s+"stopTime:"+stopTime+".last:"+(stopTime-startTime)+"");

}

public static void main(String[] args) { TestThread tt = new TestThread();

/*
 * 单线程调用测试
 * */
long startTime = System.currentTimeMillis();
System.out.println("T-single:startTime:"+startTime);
int result = 0 ;
for(int i=0;i<1000000000;i++)
result = i;
long stopTime = System.currentTimeMillis();
System.out.println("T-single:stopTime:"+stopTime+".last:"+(stopTime-startTime)+"");


thd1 t1 = new thd1(tt);
thd2 t2 = new thd2(tt);
thd3 t3 = new thd3(tt);
t1.start();
t2.start();
t3.start();
}
}class thd1 extends Thread{

private TestThread tt;
public thd1(TestThread tt){this.tt=tt;};

public void run(){

//System.out.println(currentThread().getName());//本句显示线程调用顺序,结合各线程的结束时间可以得出线程无序执行的结论

/*
 * 测试静态方法调用方式
 * */
//TestThread.stati("T-static1");

/*
 * 测试synchronized调用方式
 * */
tt.sync(3,"T-sync1");

/*
 * 测试多实例调用方式
 * */
//new TestThread().sync(1,"T-multiThread1");
}
}class thd2 extends Thread{ private TestThread tt;
public thd2(TestThread tt){this.tt=tt;};

public void run(){
//System.out.println(currentThread().getName());
//TestThread.stati("T-static2");
tt.sync(1,"T-sync2");
//new TestThread().sync(2,"T-multiThread2");
}
}class thd3 extends Thread{ private TestThread tt;
public thd3(TestThread tt){this.tt=tt;};
public void run(){
//System.out.println(currentThread().getName());
//TestThread.stati("T-static3");
tt.sync(2,"T-sync3");
//new TestThread().sync(3,"T-multiThread3");
}
}