Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It seems that what you basically want to achieve is a system that alternates control between two independent units so that each of the units gets some time to process followed by a two second waiting period (or vice versa).</p> <p>There are two main ways you could achieve this:</p> <ol> <li>Using a central control</li> <li>With autonomous communicating agents</li> </ol> <p>The first approach is a bit easier. Here, you have a central "master" component which takes care of coordinating who gets the processing time and also implements the wait times. For that approach, the two independent units do not even have to be Threads:</p> <pre class="lang-java prettyprint-override"><code>public class South { private Queue&lt;Car&gt; q = new LinkedList&lt;Car&gt;(); public void CreateQueue() { ... } public void poll() { System.out.print("The car with registration number: "); System.out.print(q.poll().getRegNum()); System.out.println(" have passed the bridge from the South side."); } } public class North { private Queue&lt;Car&gt; q = new LinkedList&lt;Car&gt;(); public void CreateQueue() { ... } public void poll() { System.out.print("The car with registration number: "); System.out.print(q.poll().getRegNum()); System.out.println(" have passed the bridge from the North side."); } } // This is the "master" class public class Main { public static void main(String[] args) { South south = new South(); North north = new North(); south.CreateQueue(); north.CreateQueue(); boolean done = false; while (!done) { try { Thread.sleep(2000); } (catch InterruptedException) { /* TODO */ } north.poll(); try { Thread.sleep(2000); } (catch InterruptedException) { /* TODO */ } south.poll(); } } } </code></pre> <p>Note that North and South do not inherit from <code>Thread</code> here, i.e., they are just plain old objects. (However, if your program is more complex and North/South are only one part of it, you might want to make Main(!) a separate thread and put the above while-loop inside the thread's <code>run</code> method, so that the rest of the program can run concurrently.)</p> <p>In the second approach, you don't have such a central control component, but the both North and South run in their own separate threads. This then requires that they coordinate who's allowed to process by communicating with each other.</p> <pre class="lang-java prettyprint-override"><code>public class SouthThread extends Thread { protected Queue&lt;Car&gt; q = new LinkedList&lt;Car&gt;(); protected North north; public void CreateQueue() { ... } public void poll() { ... } public void run() { boolean done = false; while (!done) { // wait two seconds try { Thread.sleep(2000); } (catch InterruptedException) { /* TODO */ } // process one element from the queue poll(); // notify the other thread synchronized (north) { north.notifyAll(); } // wait until the other thread notifies this one try { synchronized (this) { wait(); } } (catch InterruptedException) { /* TODO */ } } } } public class NorthThread extends Thread { protected Queue&lt;Car&gt; q = new LinkedList&lt;Car&gt;(); protected South south; public void CreateQueue() { ... } public void poll() { ... } public void run() { boolean done = false; while (!done) { // wait two seconds try { Thread.sleep(2000); } (catch InterruptedException) { /* TODO */ } // process one element from the queue poll(); // notify the other thread synchronized (south) { south.notifyAll(); } // wait until the other thread notifies this one try { synchronized (this) { wait(); } } (catch InterruptedException) { /* TODO */ } } } } public class Main { public static void main(String[] args) throws Exception { SouthThread tSouthThread = new SouthThread(); NorthThread tNorthThread = new NorthThread(); tSouthThread.north = tNorthThread; tNorthThread.south = tSouthThread; tSouthThread.CreateQueue(); tNorthThread.CreateQueue(); tSouthThread.start(); tNorthThread.start(); } } </code></pre> <p>A more general remark: since both North and South seem to be doing basically the same, there's probably no need to implement them in two separate classes. Instead, it should be sufficient to have only one class that implements the desired functionality and instantiate it twice:</p> <pre class="lang-java prettyprint-override"><code>// We can combine the functionality of North and South // in a single class public class NorthSouth { public void CreateQueue() { ... } public void poll() { ... } // etc. } public class Main { public static void main(String[] args) { NorthSouth north = new NorthSouth(); NorthSouth south = new NorthSouth(); north.CreateQueue(); south.CreateQueue(); // etc. } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload