0

I need to write an Application that extends the Thread class. My class accepts an integer(i.e. 100) when it is instantiated. (MyThread myt = new MyThread(100); ) This integer number will be the number of times that this class loops and prints a message. The message should read “The Thread is running… 100”. The 100 would be whatever number I passed into the constructor. If the number was 150, then the output should read “ The Thread is running… 100”. I should use a main method to test this class. In the main I will start 2 Threads, one Thread with 150 and one Thread with 200. I don't need to use the sleep() method for this code.

I already wrote a code, but I'm confused. Should my message be printed 100 times? I'm not sure if my code meets all requisites. I should also implement this code changing this class to use the Runnable Interface instead of Thread class

public class MyThread extends Thread {

    private int numtimes;

    public MyThread(int numtimes) {
        this.numbtimes = numbtimes;

    }

    public void run() {

        for (int i = 0; i < numbtimes; i++) {
            System.out.println("Thread Running..." + numbtimes);

        }
    }

    public static void main(String[] args) {

        MyThread mytr1 = new MyThread(150);
        mytr1.start();

        MyThread mytr2 = new MyThread(200);
        mytr2.start();
    }

}

Is that what was asked? How would you do using Runnable Interface?

7
  • If the number was 150, then the output should read “ The Thread is running… 100” - That's strange Commented Jun 27, 2019 at 1:13
  • Should my message be printed 100 times? yes/no/ask your teacher Commented Jun 27, 2019 at 1:15
  • finally,in this trivial example without a sleep, the first thread will consume all cpu and finish before the second one begins Commented Jun 27, 2019 at 1:16
  • Yes, I used the 100 because the example was using this number... The hypothesis explanation was considering the number was 100. Of course I asked him, but he didn't answer. That's why I'm asking here. Commented Jun 27, 2019 at 1:46
  • but he didn't answer. - I feel for you. BTW, my third comment is the one you pay most attention to ;-) Commented Jun 27, 2019 at 1:48

1 Answer 1

1

two way you can use. Actually the same kind. but I prefer lambda

public class StackOverFlowDemo {

/**
 * one
 * */
public static class MyRun implements Runnable {
    private int numtimes;

    public MyRun(int numtimes) {
        this.numtimes = numtimes;
    }

    @Override
    public void run() {
        for (int i = 0; i < numtimes; i++) {
            System.out.println(String.format("Thread(%s) Running... numtimes(%d), current count (%d) ",
                    Thread.currentThread().getName(),
                    numtimes, i));
        }
    }
}

/**
 * another way
 * */
public static void print(int numtimes) {
    for (int i = 0; i < numtimes; i++) {
        System.out.println(String.format("Thread(%s) Running... numtimes(%d), current count (%d) ",
                Thread.currentThread().getName(),
                numtimes, i));
    }
}

public static void main(String[] args) {
    /**
     * one
     * */
    Thread t1 = new Thread(new MyRun(150), "thread 1");
    Thread t2 = new Thread(new MyRun(200), "thread 2");
    t1.start();
    t2.start();

    /**
     * another way
     * */
    new Thread(() -> StackOverFlowDemo.print(150), "t1").start();
    new Thread(() -> StackOverFlowDemo.print(200), "t2").start();
}

}

Sign up to request clarification or add additional context in comments.

2 Comments

This is a better answer, but why not use System.out.format
ok, i got it thanks ^_^. usually I use slf4j with lombok @slf4j annotation in real project,so do not know System.out.format

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.