Results 1 to 5 of 5
  1. #1
    huma is offline Member
    Join Date
    Jun 2008
    Posts
    2
    Rep Power
    0

    Default 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?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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 04:32 PM.

  3. #3
    huma is offline Member
    Join Date
    Jun 2008
    Posts
    2
    Rep Power
    0

    Default

    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?

  4. #4
    pao
    pao is offline Member
    Join Date
    Jun 2008
    Posts
    43
    Rep Power
    0

    Default

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

  5. #5
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    6

    Default

    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.

Similar Threads

  1. Output Redirection
    By Sixtease in forum New To Java
    Replies: 8
    Last Post: 12-29-2008, 10:18 AM
  2. No output displaying
    By Rgfirefly24 in forum New To Java
    Replies: 6
    Last Post: 04-27-2008, 08:37 PM
  3. Why the output is always zero
    By mehrotra.chitij in forum New To Java
    Replies: 12
    Last Post: 04-25-2008, 04:05 AM
  4. How to redirect the output
    By Java Tip in forum java.io
    Replies: 0
    Last Post: 04-04-2008, 02:36 PM
  5. output
    By Camden in forum New To Java
    Replies: 3
    Last Post: 12-01-2007, 10:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •