Win a copy of Modern frontends with htmx this week in the Spring forum!
  • 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Threads

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another question from jdiscuss.com


public class Test extends Thread
{
String msg = "default" ;
public Test(String s)
{
msg = s;
}
public void run()
{
System.out.println(msg);
}
public static void main(String args[])
{
new Test("String1").start();
new Test("String2").start();
System.out.println("end");
}
}

Options

Select 1 correct option.

A) The program will fail to compile.


B) It will always print 'String1' 'String2' and 'end', in that order.


C) It will always print 'String1' 'String2' in random order followed by 'end'.


D) It will always print 'end' first.



I answered: B


Correct Answer given was D

Why d?
First thread start method will run first and prints the message and then second thread will start.

Correct me.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
im had used ur program as it is and compiled. when im running the program continuesely im not geting them in any certain order. one time "end" is printed first, other time "String1". i think we can't expect the result in a particular order
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The two threads will have the same priority as the main thread.
I think that will finish first because all the three threads have the same priority?

Please correct me if i'm wrong!
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI all

All the threads has the same priority, so we cannot gurantee the order of execution
 
Alpana Singh
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys
 
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That Means,
None of the given answers :
A) The program will fail to compile.
B) It will always print 'String1' 'String2' and 'end', in that order.
C) It will always print 'String1' 'String2' in random order followed by 'end'.
D) It will always print 'end' first.

is correct...
since no option says that the output cannot be predicted
 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
alpana....i have also given the question ...i suppose ..there was a option that the order is not guaranteed....not sure...
but the answer should be no order is guaranteed,......


thanks'
sri
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic