日期:2014-05-20 浏览次数:21209 次
package csdn.thread;
class CalculateThread implements Runnable
{
private short totalThread; //记录总线程数.
private int count=0; //用于累加计算.
private final int MAX=10; //最大计算值,到达程序结束.
//-----
public CalculateThread(short totalThread)
{
if(totalThread>=2||totalThread<=9) //2--9有效
{
this.totalThread=totalThread; //进行这个计算的线程总数。
}
else
{
totalThread=2;
}
}
//-----
public void run()
{
while(count<MAX)
{
try
{
Thread.sleep(10); //线程休眠一会。
}
catch(InterruptedException ie)
{
ie.printStackTrace();
}
synchronized (this) //对count操作同步。
{
String name=Thread.currentThread().getName(); //当前线程名。
char nChar=name.charAt(name.length()-1);
short number=(short)(nChar-'0'); //获得线程号。
if(count%totalThread==(number-1)&&count<MAX) //主要判断逻辑。
{
count++;
System.out.println("Current Thread is "+name+" and count is "+count);
}
}
}
}
}//end class CalculateThread.
public class ControlThread
{
public static void main(String[] args)
{
short totalThread=3;
CalculateThread ct=new CalculateThread(totalThread); //传入线程总数。
Thread thread_1=new Thread(ct,"thread_1"); //线程必需这样命名。
Thread thread_2=new Thread(ct,"thread_2");
Thread thread_3=new Thread(ct,"thread_3");
thread_1.start();
thread_2.start();
thread_3.start();
}//end main.
}//end class ControlThread。
/* 结果
Current Thread is thread_1 and count is 1
Current Thread is thread_2 and count is 2
Current Thread is thread_3 and count is 3
Current Thread is thread_1 and count is 4
Current Thread is thread_2 and count is 5
Current Thread is thread_3 and count is 6
Current Thread is thread_1 and count is 7
Current Thread is thread_2 and count is 8
Current Thread is thread_3 and count is 9
Current Thread is thread_1 and count is 10
*/
------解决方案--------------------
我是用最基本的PV操作实现这个的,用到的是Semaphore类
import java.util.concurrent.*;
public class TestThread extends Thread
{
private Res res; //要操作的数
private TestThread wakeThread; //要唤醒的线程
private Semaphore semaphore; //信号量
public TestThread(String name, Res res)
{
super(name);
this.res = res;
}
public void setWakeThread(TestThread wakeThread)
{
this.wakeThread = wakeThread;
}
public void setSemaphore(Semaphore semaphore)
{
this.semaphore = semaphore;
}
public Semaphore getSemaphore()
{
return semaphore;
}
public void run()
{
while (true)
{
try
{
semaphore.acquire(); //请求信号量,如果为0则阻塞
}
catch (InterruptedException e)
{
e.printStackTrace();
}
System.out.println(this.getName() + " " + res.getI());
res.inc();
wakeThread.getSemaphore().release(); //释放wakeThread的信号量,让wakeThread运行
}
}
public static void main(String[] args)
{
Res res = new Res();
TestThread t1 = new TestThread("Thread-1", res);
TestThread t2 = new TestThread("Thread-2", res);
TestThread t3 = new TestThread("Thread-3", res);
t1.setWakeThread(t2); //t1唤醒t2
t2.setWakeThread(t3); //t2唤醒t3
t3.setWakeThread(t1); //t3唤醒t1
t1.setSemaphore(new Semaphore(1)); //t1起始有一个信号量,其余线程没有
t2.setSemaphore(new Semaphore(0));
t3.setSemaphore(new Semaphore(0));
t1.start();
t2.start();
t3.start();
}
}
class Res
{
private int i = 0;
public synchronized void inc()
{
i++;
}
public synchronized int getI()
{
return i;
}
}