• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

i can't make this code run in ide

 
Ranch Hand
Posts: 821
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need help to get the answer for this MCQ :


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


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.
 
Sheriff
Posts: 8064
569
Mac OS X VI Editor BSD Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. You can't have two public classes in the same source file. You have MyThread and myTest.
2. Source filename should match the public class's name.

Try to fix those and see how it goes then.
 
Liutauras Vilda
Sheriff
Posts: 8064
569
Mac OS X VI Editor BSD Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure whether the code is copied exactly as it should, but none of the answers are inline with an existing code.
 
tangara goh
Ranch Hand
Posts: 821
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:1. You can't have two public classes in the same source file. You have MyThread and myTest.
2. Source filename should match the public class's name.

Try to fix those and see how it goes then.



I did put myTest in another class under the same package in the IDE but when it doesn't give me any answer listed in the 4 choices I wanted to find out more, hence this post.

 
Master Rancher
Posts: 4465
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


Do you mean this is what the output should look like:
1. Prints:
MyThread: run()
MyRunnable: start()


Add @Override before the start methods to see which ones actually are overriding a  class method.
 
tangara goh
Ranch Hand
Posts: 821
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:I'm not sure whether the code is copied exactly as it should, but none of the answers are inline with an existing code.



I have double check and it is indeed the same.  In fact, before I posted, I was worried that I have copied wrongly.

And I did make sure it is really the same cos I could not get any answer and was abit doubtful.
 
Saloon Keeper
Posts: 1277
38
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, if you made no other mistakes in what you are showing us, none of these are the correct answer.
Choose "None of the Above" if available.

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?

2. 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.


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.
 
Norm Radder
Master Rancher
Posts: 4465
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

your own .start() method, which will not be called


The start method is called for me when line 26 is executed.  That is why I suggested the OP add @Override statements to see which start is a new method and which start overrides the extended class's start method.
 
tangara goh
Ranch Hand
Posts: 821
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:

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


Do you mean this is what the output should look like:
1. Prints:
MyThread: run()
MyRunnable: start()


Add @Override before the start methods to see which ones actually are overriding a  class method.



At which start method should I add the @Override ?

 
Norm Radder
Master Rancher
Posts: 4465
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try with both to see
which is an override (no error)
and which one is not an override(will cause a compiler error)
 
Jesse Silverman
Saloon Keeper
Posts: 1277
38
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As this is Beginning Java forum, I would point out two mistakes in the code that the poster should have as take-home messages:

If you are trying to sub-class Thread and you have overloaded .start() you are either a super-advanced programmer that reads the JLS and class library sources for fun, or a beginner who has gotten confused and made a serious mistake:


If you are following the "Good Practices For Beginners" advice, then doing this:


Is harmless, but pointless and useless.  You might as well have called the method stare() or stars()

I think these are the points that the MCQ is trying to confirm the test-taker understands, in a somewhat confusing way.
 
Norm Radder
Master Rancher
Posts: 4465
38
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

you have overloaded .start()


Method Overloading is a feature that allows a class to have more than one method having the same name, if their argument lists are different.


Override


When a method in a subclass has the same name, same parameters or signature, and same return type(or sub-type) as a method in its super-class,

 
Jesse Silverman
Saloon Keeper
Posts: 1277
38
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, I didn't see that I used the wrong one in that sentence, but I did.

If you DO override .start() in a sub-class of Thread you are either very advanced or goofed and are in big trouble.

Writing a .start() method in an arbitrary class that implements Runnable by itself won't normally do anything, as the convention is to call the .start() method on the Thread and passing in the Runnable instance as a parameter.

I think that the OP is looking at a question where they are trying to trip you up to test/ensure you know the basics very solidly, which I suppose is fair game for certifications.

It is not a good thing to be looking at while still shaky on the basics, IMHO.
 
tangara goh
Ranch Hand
Posts: 821
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry guys, it is my mistake : I left out a thread.start() at the public void main there and this is right after myThread.start()
;

 
Creativity is allowing yourself to make mistakes; art is knowing which ones to keep. Keep this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic