Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How do I instantiate two threads of the same object, and have the objects print different things

  1. #1
    Junior Member
    Join Date
    Aug 2018
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How do I instantiate two threads of the same object, and have the objects print different things

    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?

    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();
       }
     
     }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,459
    Thanks
    52
    Thanked 2,313 Times in 2,269 Posts

    Default Re: How do I instantiate two threads of the same object, and have the objects print different things

    how do I make a default constructor set two different outputs
    One way to have different results in a constructor would be to have a static variable the constructor could use to determine what to do.
    The constructor would change the variable's value when it executes so that the next call to the constructor would see a different value.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Trying to figure out how to print multiple things on same page
    By GoodbyeWorld in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 9th, 2014, 04:27 PM
  2. Building DOM object from multiple threads
    By gdev in forum What's Wrong With My Code?
    Replies: 6
    Last Post: May 22nd, 2014, 04:19 PM
  3. Replies: 1
    Last Post: October 25th, 2013, 11:43 PM
  4. Problem sharing objects between threads
    By pepzi999 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 18th, 2012, 05:02 PM
  5. Two threads waiting on a object Monitor
    By pandiri in forum Threads
    Replies: 1
    Last Post: May 24th, 2012, 12:34 PM

Tags for this Thread