package com.thread;
public class ThreadTest {
public static void main(String args[])
{
ThreadTest t = new ThreadTest();
Thread thread1 = new Thread(new MeThead(t,0));
Thread thread2 = new Thread(new MeThead(t,1));
thread1.start();
thread2.start();
}
public synchronized void printA()
{
System.out.print(Param.i+"a");
Param.i++;
}
public synchronized void printB()
{
System.out.print(Param.i+"b");
Param.i++;
}
}
/**
* 设置成全局静态变量。
* @author hbx
*
*/
class Param{
public static int i=0;//两个线程每打印一次,i进行自增一次,跟MAX比较,只要小于MAX,则一直打印
public static final int MAX=20;
}
class MeThead implements Runnable{
ThreadTest t;
int method=0;
public MeThead(ThreadTest t,int method)
{
this.t=t;
this.method=method;
}
@Override
public void run() {
// TODO Auto-generated method stub
while(Param.i
{
if(method==0)
{
//System.out.println("-"+Param.i+"a-");
t.printA();
}
else if(method==1)
{
//System.out.println("-"+Param.i+"b-");
t.printB();
}
}
}
}