Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>1). You want your code to compile.</p> <p>Your compilation problems are because you're to declare your run methods within the main method. Separate you method declarations and then just use main to create &amp; run your threads.</p> <p>2). You want to have two threads operating concurrently. </p> <p>This means we need to either define two different thread classes or a single thread with logic to handle both incrementing and decrementing. Andrew Mao's answer gives you a start on how you might define two separate threads. My code above uses a single definition for a run method that uses a parameter to work out which way to go.</p> <p>3). You want to have two threads operating concurrently on the same data, because while they're incrementing/decrementing separately, they need to check for collisions.</p> <p>An easy way to do this is to create the objects you want to work on (num1 and num2) in your main method and then just pass references to these objects to your threads. Example in my code above.</p> <p>4). You want to test things</p> <p>All of java.lang.* is kind of assumed to be imported automatically. No harm in putting it there for clarity but auto-importer commands on your dev tool of choice will probably remove it as redundant.</p> <pre><code>public class ThreadExperiment implements Runnable { /* these fields are unique to each instance of ThreadExperiment */ private int increment = 0; /* these are used to point to the original num1 and num2 instances created in your main method */ private Integer myNumber; private Integer theOtherNumber; /** * Constructor. */ public ThreadExperiment(int increment, Integer num1Ref, Integer num2Ref) { this.increment = increment; this.myNumber = num1Ref; this.theOtherNumber = num2Ref; } @Override public void run() { do { myNumber += increment; try { Thread.sleep(400); } catch (InterruptedException e) { } System.out.println(Thread.currentThread().getName() + " -- " + myNumber); } while (!myNumber.equals(theOtherNumber)); } /** * Your static main method used to instantiate &amp; start threads */ public static void main(String[] args) { Integer num1 = 0; Integer num2 = 10; Thread t = new Thread(new ThreadExperiment(1, num1, num2), "Thread 1"); Thread t2 = new Thread(new ThreadExperiment(-1, num2, num1), "Thread 2"); t.start(); t2.start(); } } </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