Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    _tony is offline Member
    Join Date
    Jul 2008
    Posts
    3
    Rep Power
    0

    Default Java Threads Interview Question

    On a recent interview, I was given the code below.

    public class ThreadsTest {

    public static void main(String[] args) {
    Thread_1 aaa = new Thread_1("AAAAAAAAAAAAAAA");
    Thread_2 bbb = new Thread_2("BBBBBBBBBBBBBBB");
    Thread x = new Thread(aaa);
    Thread y = new Thread(bbb);
    x.start();
    y.start();



    }
    }

    class Thread_1 implements Runnable {
    String str;
    public Thread_1(String s) { str = s; }
    public void run() {

    int n = 0;
    while (n<100) {
    System.out.print(str);
    System.out.println();
    n++;
    }

    }
    }

    class Thread_2 implements Runnable {
    String str;
    public Thread_2(String s) { str = s; }
    public void run() {

    int n = 0;
    while (n<100) {
    System.out.print(str);
    System.out.println();
    n++;
    }


    }
    }

    I was told to use Java's synchronization constructs so that the A lines will not be interleaved with the B lines. Anyone has any idea how to do this?

  2. #2
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    probably synchronized(this)

    Does the interviewer have any experience synchronizing on this or are you mostly interested in getting your first job?
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  3. #3
    _tony is offline Member
    Join Date
    Jul 2008
    Posts
    3
    Rep Power
    0

    Default

    The interviewer requires some experience with multithreading.

    I tried using the synchronization, but it did not work and I was told I was wrong. Could you show how synchronizing on this will work?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    How much can you modify the code?
    To have one thread run, printing out its lines and then the other thread run, printing out its lines, you could use a synchronized(<object-reference) {} block. The first thread into its synchronized block will block the second one from entering its block until 6he first one leaves or does a wait(). The <object-reference> must be the same object for both synch blocks. One Q&D way is add a static object in the main class that the others can reference. Otherwise, pass a reference via the Threads' constructors.

    public static Object monitor = new Object();

  5. #5
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Exclamation Norm, this one's yours.

    Norm, I could write this but then that's what the interviewer is trying to discover. Not an environment I am suited for. This one's yours.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    I usually don't write code for assignments. I tried above to describe how to do it and will wait on the OP to respond.

    PS.I did take his code and modify as mentioned and had it work as desired.

  7. #7
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    My main question to this, is how strictly the "interleaving" has to be prevented?

    Should it be that all 100 "AAAA" come before any "BBBB"?

    Or is it only that you should not get any blocks such as "AAAAAAAABBBBBBBBB" followed by two newlines (which could and would definately happen with the way that is written).

    If the second case, then, of course, the synchronized block must be around only the two print statements and not around the while loop. Or a synchronized around the while with notify and wait positioned around the prints (but that could also, easily lead to hung threads).

    Better would be the "new" concurrent package and to obtain a lock on the object directly before printing and release it directly after printing (or directly before and after the while loop if the first case).

    And, regardless of which it is, the object on which they are synchronizing must be the same Object visible from both classes, which will mean a larger change to the code to provide that Object in such a way that both have access to it (hopefully without it being some public static object inside of ThreadTest).

  8. #8
    skaspersen is offline Member
    Join Date
    Jul 2008
    Posts
    31
    Rep Power
    0

    Default

    The easiest way would be to place a synchronized block around the print statements with the synchronized object being System.out

  9. #9
    _tony is offline Member
    Join Date
    Jul 2008
    Posts
    3
    Rep Power
    0

    Default

    Thanks everyone for the replies. I think what the interviewer had in mind was not just a simple synchronized block, but also incorporation of wait() and notify() so that the threads could communicate. I think masijade's solution is the closest one to what the interviewer had in mind.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    Not sure how you'd use wait() and notify() without making it more complicated. System.out is the resource being shared. To use wait() you'd need a synchronzied method that each class would call to get permission to use the resource. That would entail a program switch/flag to allow the method to test/set if the the resource was being used. If it was NOT being used, set the switch and use the resource. On completion of using the resource, clear the switch and do a notifyAll() in case other threads are waiting.
    If the resource was being used, the second thread would wait().
    When the notify() came, the second thread could continue, etc

    Seems like a lot of code that doesn't do any more than using a synchronized() {} block on a single/common object to control the printing loops.

  11. #11
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default thread communication

    Quote Originally Posted by _tony View Post
    ...but also incorporation of wait() and notify() so that the threads could communicate......
    Probably as skaspersen directs, the avoidance of blocks such as "AAAAAAAABBBBBBBBB" followed by two newlines can be as well AAAABBBBABABAAABABA and hung machines. Most people use wait() and notifiy. That is fine for typical application, and is likely what your interviewer or instructor has in mind. masijade gives deeper treatment, but an abbreviated look at your code does not show how Thread_2 implements interaction with Thread_1. As written, reliance on sync objects beyond a common shared scope is usually termed 'semaphore' in cs.

    At least that is better than interview questions about maritime computers in USO Canteen FReeper Style ~ Julius Caesar: The Gallic Wars ~ September 16, 2003. There are so many approaches, I will leave you to see what approaches you may consider.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  12. #12
    peter_lawrey is offline Member
    Join Date
    Mar 2008
    Posts
    10
    Rep Power
    0

    Default

    You need to lock on a common object
    e.g. put this block around the contents of the run method.
    synchronize(ThreadsTest.class) {

    However since the use of threads is redundant here you could just replace x.start(); with x.run();

  13. #13
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default redundant?

    Quote Originally Posted by peter_lawrey View Post
    ....However since the use of threads is redundant here you could just replace x.start(); with x.run();
    Correct, but that would be beyond most interviewer's skills.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  14. #14
    peter_lawrey is offline Member
    Join Date
    Mar 2008
    Posts
    10
    Rep Power
    0

    Default

    Quote Originally Posted by Nicholas Jordan View Post
    Correct, but that would be beyond most interviewer's skills.
    :D
    Yes, its important not to confuse the interviewer as this is almost always bad for you.

  15. #15
    JPRABHU is offline Member
    Join Date
    Jul 2008
    Posts
    3
    Rep Power
    0

    Default

    public class MyAnswer
    {
    public static void main(String[] args) {
    Thread_1 aaa = new Thread_1("AAAAAAAAAAAAAAA");
    Thread_2 bbb = new Thread_2("BBBBBBBBBBBBBBB");
    Thread x = new Thread(aaa);
    Thread y = new Thread(bbb);
    x.start();
    y.start();



    }
    }



    class Thread_1 implements Runnable {
    String str;
    public Thread_1(String s) { str = s; }



    public void run()
    {
    synchronized(Thread_2.class)
    {
    int n = 0;
    while (n<100) {
    System.out.print(str);
    System.out.println();
    n++;
    }

    }
    }
    }

    class Thread_2 implements Runnable {
    String str;
    public Thread_2(String s) { str = s; }

    public void run() {
    synchronized(Thread_2.class)
    {
    int n = 0;
    while (n<100) {
    System.out.print(str);
    System.out.println();
    n++;
    }

    }
    }
    }

  16. #16
    JPRABHU is offline Member
    Join Date
    Jul 2008
    Posts
    3
    Rep Power
    0

    Default

    public class MyAnswer
    {
    public static void main(String[] args) {
    Thread_1 aaa = new Thread_1("AAAAAAAAAAAAAAA");
    Thread_2 bbb = new Thread_2("BBBBBBBBBBBBBBB");
    Thread x = new Thread(aaa);
    Thread y = new Thread(bbb);
    x.start();
    y.start();



    }
    }



    class Thread_1 implements Runnable {
    String str;
    public Thread_1(String s) { str = s; }



    public void run()
    {
    synchronized(Thread_2.class)//-----> acquired lock on class

    {
    int n = 0;
    while (n<100) {
    System.out.print(str);
    System.out.println();
    n++;
    }

    }
    }
    }

    class Thread_2 implements Runnable {
    String str;
    public Thread_2(String s) { str = s; }

    public void run() {
    synchronized(Thread_2.class)//-----> acquired lock on class
    {
    int n = 0;
    while (n<100) {
    System.out.print(str);
    System.out.println();
    n++;
    }

    }
    }
    }



    According to me, If we acquire lock on a class is the better solution

  17. #17
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Exclamation deadlock

    Quote Originally Posted by JPRABHU View Post
    According to me, If we acquire lock on a class is the better solution
    That is correct, but with two synchronized( Object ) we have the potential for cross-lock if poster try to do it this way:
    Java Code:
    synchronized(firstObject){
    synchronized(secondObject){
    and does that in two places in the code.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  18. #18
    Biswa is offline Member
    Join Date
    Sep 2008
    Posts
    3
    Rep Power
    0

    Default hI

    hI,
    This is Biswanath

  19. #19
    Biswa is offline Member
    Join Date
    Sep 2008
    Posts
    3
    Rep Power
    0

    Default jsp

    Hi,
    How can we connect one jsp page to multiple jsp page?

  20. #20
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default use servlet

    In general, a jsp may be a landing page for a servlet, but most of the workload should be passed to a servlet. Many reasons.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

Page 1 of 2 12 LastLast

Similar Threads

  1. threads question
    By sandor in forum Threads and Synchronization
    Replies: 9
    Last Post: 02-07-2009, 08:57 PM
  2. Job Interview About J2EE 2007
    By pazuvn in forum Advanced Java
    Replies: 1
    Last Post: 05-14-2008, 02:45 AM
  3. We need Java Programmers to interview
    By jayson_sabale in forum Forum Lobby
    Replies: 4
    Last Post: 05-05-2008, 09:34 AM

Posting Permissions

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