开发者

Trying to loop 3 threads in a specific order everytime

开发者 https://www.devze.com 2023-02-20 05:26 出处:网络
My question is how do I make a thread run, then after that another run, then after that another run again, then it repeats itself.

My question is how do I make a thread run, then after that another run, then after that another run again, then it repeats itself.

I have a main file

    private static ThreadManager threadManager;

public static void main(String[] args)
{
    threadManager = new ThreadManager();
}

Then I have a ThreadManager class

public class ThreadManager {
 public static final Object lock1 = new Object();

public static ConcThread CT = new ConcThread();
public static SocketThread sThread = new SocketThread();
public static PacketThread packetThread = new PacketThread();

 public ThreadManager() {
    try {
        synchronized (lock1) {
            packetThread.packetThread.start();
                lock1.wait();
            CT.concThread.start();
                lock1.wait();
开发者_如何学Go            sThread.socketThread.start();
        }
    } catch (InterruptedException e) {
            e.printStackTrace();
    }
 }

Then I have 3 threads

        public class PacketThread implements Runnable {

        public Thread packetThread = new Thread(this);

        public void run() {
            while (true) {
                try {
                    synchronized (ThreadManager.lock1) {
                        //DoThing1
                        synchronized (this) {
                            ThreadManager.lock1.notifyAll();
                        }
                        ThreadManager.lock1.wait();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public class ConcThread implements Runnable {

    public Thread concThread = new Thread(this);

    public void run() {
        while (true) {
            synchronized (ThreadManager.lock1) {
                try {
                    //dothing2
                synchronized (this) {
                    ThreadManager.lock1.notifyAll();
                }
                ThreadManager.lock1.wait();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } 
    }
}

    public class SocketThread implements Runnable {

    public Thread socketThread = new Thread(this);

    public void run() {
        while (true) {
            synchronized (ThreadManager.lock1) {
                try {
                    //dothing3
                    synchronized (this) {
                        ThreadManager.lock1.notifyAll();
                    }
                    ThreadManager.lock1.wait();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
         }
    }
}


Rather than having a single lock shared between the three threads (which is in-determinant on which thread will pick up after a thread releases the lock), have three separate semaphore/locks, where thread #1 unlocks a semaphore for thread #2 after its task is complete, thread #2 unlocks the semaphore for thread #3, and thread #3 unlocks the semaphore for thread #1.

So it would look something like:

  1. Thread #1 runs (thread #2 and thread #3 are currently blocked)
  2. Thread #1 completes
  3. Thread #1 unlocks semaphore for thread #2
  4. Thread #1 blocks
  5. Thread #2 runs
  6. Thread #2 completes
  7. Thread #2 unlocks semaphore for thread #3
  8. Thread #2 blocks
  9. Thread #3 runs
  10. Thread #3 completes
  11. Thread #3 unlocks semaphore for thread #1
  12. Thread #3 blocks

Hope this helps,

Jason


Have you considered looking at Runnable to identify the chunks of work you have, and an appropriate Executor to control what runs when?

0

精彩评论

暂无评论...
验证码 换一张
取 消