Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-21-2008, 04:39 PM
Member
 
Join Date: Jun 2008
Posts: 2
huma is on a distinguished road
What will be output and why
1. public class TwoThreads {
2
3. private static Object resource = new Object();
4.
5. private static void delay(long n) {
6. try { Thread.sleep(n); }
7. catch (Exception e) { System.out.print(”Error “); }
8. }
9
10. public static void main(String[] args) {
11. System.out.print(”StartMain “);
12. new Thread1().start();
13. delay(1000);
14. Thread t2 = new Thread2();
15. t2.start();
16. delay(1000);
17. t2.interrupt
18. delay(1000);
19. System.out.print(”EndMain “);
20. }
21.
22. static class Thread 1 extends Thread {
23. public void run() {
24. synchronized (resource) {
25. System.out.print(”Startl “);
26. delay(6000);
27. System.out.print(”End1 “);
28. }
29. }
30. }
31.
32. static class Thread2 extends Thread {
33. public void run() {
34. synchronized (resource) {
35. System.out.print(”Start2 “);
36. delay(2000);
37. System.out.print(”End2 “);
38. }
39. }
40. }
41. }
Assume that sleep(n) executes in exactly m milliseconds, and all other
code executes in an insignificant amount of time. What is the output if
the main() method is run?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 06-21-2008, 05:22 PM
Senior Member
 
Join Date: Jun 2008
Posts: 180
Fubarable is on a distinguished road
Most of us here know the answer, but better first would be for you to answer the questions yourself: when you did this, what happened? And what currently is your explanation for this?

Good luck

Last edited by Fubarable : 06-21-2008 at 05:32 PM.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 06-23-2008, 05:40 PM
Member
 
Join Date: Jun 2008
Posts: 2
huma is on a distinguished road
My analysis is straight forward it should print
start1 endmain end1 start2 end2

In what case can Thread.sleep() can through exception? Is there any case here when exception can be thrown?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 06-26-2008, 10:53 PM
pao pao is offline
Member
 
Join Date: Jun 2008
Posts: 41
pao is on a distinguished road
I think Interrupted exception could be called on a sleeping thread. Read the thread api I dont use it much. Also I dont know what the output would be, I dont use threading often. As I guess I would say you basically cant tell what the output would be, it could be in any order based on the thread scheduler (I would have to read the api though and I cant be bothered right now)? I know with the thread api nothing is guaranteed and a lot is down to your jvm implementation.

If anyone wants to answer for me then I wouldnt mind knowing.

By the way to the OP, if your doing a java course why dont you just run the program and find out what the order is and if possibly any Exceptions are thrown. If it were my assignment that would be the first thing I would do, well after reading the API and a relevant thread section in a good java book...
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 06-26-2008, 11:14 PM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 240
fishtoprecords is on a distinguished road
Quote:
Originally Posted by huma View Post
My analysis is straight forward it should print
start1 endmain end1 start2 end2

In what case can Thread.sleep() can through exception?
Why don't you just run the code? Its small.

Its not clear how deterministic the code is, in general threaded code runs and does its thing, and you don't know what order will be used unless you use java.util.concurrent.Semaphore or similar constructs.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
No output displaying Rgfirefly24 New To Java 6 04-27-2008 09:37 PM
Why the output is always zero mehrotra.chitij New To Java 12 04-25-2008 05:05 AM
How to redirect the output Java Tip java.io 0 04-04-2008 03:36 PM
Output Redirection Sixtease New To Java 2 03-22-2008 10:59 AM
output Camden New To Java 3 12-01-2007 11:34 PM


All times are GMT +3. The time now is 05:02 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org