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 07-16-2008, 10:11 PM
Member
 
Join Date: Jul 2008
Posts: 3
_tony is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-16-2008, 10:14 PM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 409
Nicholas Jordan is on a distinguished road
probably synchronized(this)

Does the interviewer have any experience synchronizing on this or are you mostly interested in getting your first job?
__________________
Please provide your feedback on our
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-16-2008, 10:20 PM
Member
 
Join Date: Jul 2008
Posts: 3
_tony is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-17-2008, 12:35 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SW MO, USA
Posts: 862
Norm is on a distinguished road
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();
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-17-2008, 01:13 AM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 409
Nicholas Jordan is on a distinguished road
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.
__________________
Please provide your feedback on our
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 07-17-2008, 04:55 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SW MO, USA
Posts: 862
Norm is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 07-17-2008, 10:04 AM
Senior Member
 
Join Date: Jun 2008
Posts: 161
masijade is on a distinguished road
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).
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 07-17-2008, 12:22 PM
Member
 
Join Date: Jul 2008
Posts: 31
skaspersen is on a distinguished road
The easiest way would be to place a synchronized block around the print statements with the synchronized object being System.out
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 07-17-2008, 04:34 PM
Member
 
Join Date: Jul 2008
Posts: 3
_tony is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 07-17-2008, 05:48 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SW MO, USA
Posts: 862
Norm is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 07-17-2008, 06:02 PM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 409
Nicholas Jordan is on a distinguished road
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.
__________________
Please provide your feedback on our
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 07-19-2008, 11:04 AM
Member
 
Join Date: Mar 2008
Posts: 3
peter_lawrey is on a distinguished road
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();
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 07-21-2008, 04:01 AM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 409
Nicholas Jordan is on a distinguished road
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.
__________________
Please provide your feedback on our
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 07-21-2008, 09:18 AM
Member
 
Join Date: Mar 2008
Posts: 3
peter_lawrey is on a distinguished road
Quote:
Originally Posted by Nicholas Jordan View Post
Correct, but that would be beyond most interviewer's skills.

Yes, its important not to confuse the interviewer as this is almost always bad for you.
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 07-22-2008, 09:27 AM
Member
 
Join Date: Jul 2008
Posts: 3
JPRABHU is on a distinguished road
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++;
}

}
}
}
Bookmark Post in Technorati
Reply With Quote
  #16 (permalink)  
Old 07-22-2008, 09:31 AM
Member
 
Join Date: Jul 2008
Posts: 3
JPRABHU is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #17 (permalink)  
Old 07-22-2008, 04:38 PM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 409
Nicholas Jordan is on a distinguished road
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:
Code:
synchronized(firstObject){ synchronized(secondObject){
and does that in two places in the code.
__________________
Please provide your feedback on our
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
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
threads question sandor Threads and Synchronization 7 07-21-2008 05:04 PM
Job Interview About J2EE 2007 pazuvn Advanced Java 1 05-14-2008 03:45 AM
We need Java Programmers to interview jayson_sabale Forum Lobby 4 05-05-2008 10:34 AM
Hibernate interview Q&A arulk Java Tips 0 04-24-2008 06:46 AM
Java Threads JavaForums Java Blogs 0 12-17-2007 01:21 PM


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


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