0

The goal: So I have a runnable class ThisThat. I instantiate two threads of ThisThat. One prints "This" and one prints "That". The main class is not supposed to determine what it prints.

The question: how do I make a default constructor set two different outputs for two threads of the same class? What can be improved? How can I make it only print this or that instead of both simultaneously?

Desired end result would be a program that runs for about 10 seconds and prints either this or that 10 times. Current output is "this" "that" at the same time, waits about 10 seconds and then repeats 10 times.

import java.util.Random;


public class ThisThat implements Runnable {

private String output;
private int threadNum;

public ThisThat() {
    output = "";
}
 public ThisThat(int t_Num) { 
    threadNum = t_Num;
    setThisOrThat(threadNum);
}


public void setThisOrThat(int num) {
    if (num == 1) {
        output = "this";
    } else if (num == 2) {
        output = "that";
    } else {
        Random random = new Random();
        int randNum = random.nextInt((3) + 1);
        setThisOrThat(randNum);
    }
}
@Override
public void run() {
         for (int i=1; i <= 10; i++) {
                         try {
                             System.out.println(getOutput());
                            Thread.sleep((int)(800));
                          }
                            catch(InterruptedException e) {
                                 System.err.println(e);
                          }   

             }


  }


public String getOutput() { return output; }
public void setOutput(String output) { this.output = output; }

}

class Main {

public static void main(String args[]) {


  Thread thread1 = new Thread(new ThisThat(1));
  Thread thread2 = new Thread(new ThisThat(2)); 

  thread1.start();
  thread2.start();
   }

 }
  • Can you elaborate on what your desired end result is? – Daniel A. Thompson Aug 22 '18 at 21:27
  • It's not clear what isn't working about this code. – Louis Wasserman Aug 22 '18 at 21:28
  • Well, I would just use `public ThisThat(String output) { this.output = output; }` – Tom Aug 22 '18 at 21:29
  • `random.nextInt((3) + 1)` should surely be `random.nextInt(2) + 1;`, otherwise you will recursively call with parameter value 0 or 3 sometimes, resulting in another recursive call. – Andy Turner Aug 22 '18 at 21:29
  • @Tom, he said the main class is not supposed to determine what the threads print. – John Bollinger Aug 22 '18 at 21:30
  • wait, now that you've updated your question, youre looking for it to either print: `"this, this, this, this,..........." OR "that, that, that, that, that, that......"` – Dan Aug 22 '18 at 21:35
  • Upadate your `Random` logic as described in the comments, and put that in your `ThisThat` class. Use that to decide the `AtomicReference`s value, which will be given to each thread instance. – Dan Aug 22 '18 at 21:36
  • @Dan I appreciate your help so far. Yes, it's supposed to print like "this" (wait 10 seconds), "that" (wait 10 seconds).. etc... But, I tried to to use synchronization, and my professor said I should not be using synchronization. – gatorade2131 Aug 22 '18 at 21:40
  • You'll have to create a `Lock` (`Object lock = new Object()`) in your `ThisThat` class that you'll synchronize on. Then you'll have to call `'notifyAll` once you've printed. – Dan Aug 22 '18 at 21:43
  • @Dan Synchronization is not the same as a lock? I used a lock, asked my professor if it was okay and he said "the spec does not ask for synchronization", while also saying it should only print one or the other. Again, I appreciate your help, I'm trying my best to summarize my issue and not use the homework question. – gatorade2131 Aug 22 '18 at 21:47

1 Answers1

1

One solution is to update the constructor to not take in anything from Main, then create a static volatile or Atomic property within your ThisThat class that is basically a counter changing the values for each thread instance.

Dan
  • 979
  • 1
  • 8
  • 29
  • Do you have any kind of example for this? I'm new to this thread stuff, I'm attempting to follow this https://stackoverflow.com/questions/9749746/what-is-the-difference-between-atomic-volatile-synchronized but I do not really understand it. – gatorade2131 Aug 22 '18 at 21:49
  • @gatorade2131 - read the accepted answer, it's pretty specific in the differences. – Dan Aug 22 '18 at 21:56
  • Please take a look at https://stackoverflow.com/questions/17050869/printing-a-statement-using-threads-in-java – Dan Aug 22 '18 at 22:53