当前位置: 代码迷 >> J2SE >> 多线程可控么?解决方案
  详细解决方案

多线程可控么?解决方案

热度:3   发布时间:2016-04-24 01:11:08.0
多线程可控么?
如在一个主线程中,开启3条线程。如何控制该3条线程的顺序,
如 :
3条线程共享资源N。
要求输出:
Thread-1 n++
Thread-2 n++
Thread-3 n++

Thread-1 n++
Thread-2 n++
Thread-3 n++

Thread-1 n++
Thread-2 n++
Thread-3 n++

Thread-1 n++
Thread-2 n++
Thread-3 n++



------解决方案--------------------
可以实现这样的输出,但大部分线程都白费了.
代码如下,供参考:
Java code
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  1Current Thread is thread_2 and  count is  2Current Thread is thread_3 and  count is  3Current Thread is thread_1 and  count is  4Current Thread is thread_2 and  count is  5Current Thread is thread_3 and  count is  6Current Thread is thread_1 and  count is  7Current Thread is thread_2 and  count is  8Current Thread is thread_3 and  count is  9Current Thread is thread_1 and  count is  10 */
------解决方案--------------------
我是用最基本的PV操作实现这个的,用到的是Semaphore类
Java code
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;    }}
  相关解决方案