Results 1 to 5 of 5
Thread: What will be output and why
- 06-21-2008, 03:39 PM #1
Member
- Join Date
- Jun 2008
- Posts
- 2
- Rep Power
- 0
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?
-
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 luckLast edited by Fubarable; 06-21-2008 at 04:32 PM.
- 06-23-2008, 04:40 PM #3
Member
- Join Date
- Jun 2008
- Posts
- 2
- Rep Power
- 0
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?
- 06-26-2008, 09:53 PM #4
Member
- Join Date
- Jun 2008
- Posts
- 43
- Rep Power
- 0
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...
- 06-26-2008, 10:14 PM #5
Similar Threads
-
Output Redirection
By Sixtease in forum New To JavaReplies: 8Last Post: 12-29-2008, 10:18 AM -
No output displaying
By Rgfirefly24 in forum New To JavaReplies: 6Last Post: 04-27-2008, 08:37 PM -
Why the output is always zero
By mehrotra.chitij in forum New To JavaReplies: 12Last Post: 04-25-2008, 04:05 AM -
How to redirect the output
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 02:36 PM -
output
By Camden in forum New To JavaReplies: 3Last Post: 12-01-2007, 10:34 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks