Home > OS >  simple problems in multi thread in java
simple problems in multi thread in java

Time:09-21

I need help to get the answer for this :

The question asked what is the output of the following codes?

package tryScope;
 
public class MyThread extends Thread{
    public void run() {
        System.out.println("MyThread: run()");
    }
    public void start() {
        System.out.println("MyThread: start()");
    }
}
 
class MyRunnable implements Runnable{
    public void run() {
        System.out.println("MyRunnable: run()");
    }
    public void start() {
        System.out.println("MyRunnable: start()");
    }
}
 
public class myTest {
    public static void main(String[] args) {
        MyThread myThread = new MyThread();
        MyRunnable myRunnable = new MyRunnable();
        Thread thread = new Thread(myRunnable);
        myThread.start();
    }
}


So, I am suppose to choose 1 correct answer but I can't even make it run. I would like to verify if there is something wrong with the code the question is set?

Here are the 4 choices:

  1. Prints: MyThread: run() followed by MyRunnable: start()
  2. Prints: MyThread: start() followed by MyRunnable:start()
  3. Prints: MyThread: run() followed by MyRunnable:run()
  4. Prints: MyThread: start() followed by MyRunnable:run()

Please let me know which is the correct answer and if there is something wrong with the code.

Tks.

CodePudding user response:

I think this code is just stupid. The output should be "MyThread: start()" and nothing else.

Thats because in the class MyThread the start method is overwritten. So there's no Thread starting at all. You can try modify the method like this:

public class MyThread extends Thread{
    public void run() {
        System.out.println("MyThread: run()");
    }
    public void start() {
        System.out.println("MyThread: start()");
        super.start();
    }
}

Then the ouput should be something like "MyThread: start()" followed by "MyThread: run()"

To achieve this: MyThread: start() followed by MyRunnable:run() add in the main-method the line:

myRunnable.run()

Note: You shouldn't override the start method in Thread at all, as mentioned above from Hejday. This method handles the intern creation and starting of the Thread. The Thread will then execute it's run method see Thread.

CodePudding user response:

I haven't gotten back to studying this for a long time, but I believe I remember what they probably want you to know from this one.

There are at least two ways you can create and run threads with some starting code you wish to run.

  1. You can make your class a subclass of Thread: If you choose this way, you must override the .run() method, and normally should not mess with the .start() method. The .start() method contains some interesting systems code that you may want to look at some day, but is way beyond the scope of beginning threads usage. Really advanced users may sometimes have cause to override it, but nobody else should. Unless it contains precisely the right code, your subclass of thread will never actually start, and the .run() method will never execute in any thread at all.

To start an instance of your sub-class of Thread, you call its .start() method, which you wisely did not override, because it contains tricky, magic code that would boggle your mind if you looked at it. What you do need to know is that this code will run the code in your .run() method in an appropriate new thread that was created in your constructor.

To run in this manner, comment out the inappropriate override of .start() for MyThread class

Now, many people will say that tho it is legal to do this, it is really awkward, limiting and not very good style to be sub-classing Thread except for Advanced Usages where you mean to actually change how threads work, which most users may never even learn to or need to do.

Nevertheless, you should know this basic approach described above because you will see it on exams and in other people's code.

If that is legal, but not recommended, then how do most people say you should get parts of your code running in other threads?

  1. Use the basic Thread class from the library, passing it an instance of Runnable. Don't derive from / subclass Thread at all in the class containing the code you wish to run. Simply implement Runnable interface and override the .run() method. There is no need or point in implementing your own .start() method, which will not be called, the question seems to want to check that you know this.
MyRunnable myRunnable = new MyRunnable();
Thread thread = new Thread(myRunnable);
thread.start();

Where did the question come from? It does test solid knowledge of the basics, but by putting in wrong code that you should never write, and then asking what will happen, which is a somewhat confusing way of teaching, but fair game on cert exams that want to make sure you know what's what.

  • Related