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 5 of 5

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
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 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.

  3. #3
    Junior Member
    Join Date
    Apr 2020
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    import java.util.concurrent.locks.ReentrantLock;

    public class Thread1 {

    public static ReentrantLock lock = new ReentrantLock();

    public static void main(String[] args) {
    Thread t = new Thread(new Runnable() {
    public void run() {
    System.out.println("A Trying to lock...");
    lock.lock();
    System.out.println("A Locked...");
    try {
    Thread t = new Thread(new Runnable() {
    public void run() {
    System.out.println("B Trying to lock...");
    lock.lock();
    System.out.println("B Must not print");
    try {
    } finally {
    System.out.println("B Trying to unlock...");
    lock.unlock();
    System.out.println("B Unlocked...");
    }
    }
    });
    t.start();
    try {
    t.join();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    } finally {
    System.out.println("A Trying to unlock...");
    lock.unlock();
    System.out.println("A Unlocked...");
    }
    }
    });
    t.start();
    }

    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

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

    What happens when you compile and execute the code?
    What do you want the program to do that is different?
    Post an example of the desired output.
    Also add some comments to the code describing what the program is supposed to do.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5

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

    Have you solved this problem?

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, 03: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, 03:19 PM
  3. Replies: 1
    Last Post: October 25th, 2013, 10: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, 04:02 PM
  5. Two threads waiting on a object Monitor
    By pandiri in forum Threads
    Replies: 1
    Last Post: May 24th, 2012, 11:34 AM

Tags for this Thread