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

07-16-2008, 11:11 PM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 3
|
|
|
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?
|
|

07-16-2008, 11:14 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Southwest
Posts: 789
|
|
|
probably synchronized(this)
Does the interviewer have any experience synchronizing on this or are you mostly interested in getting your first job?
__________________
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
|
|

07-16-2008, 11:20 PM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 3
|
|
|
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?
|
|

07-17-2008, 01:35 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
|
|
|
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();
|
|

07-17-2008, 02:13 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Southwest
Posts: 789
|
|
|
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.
__________________
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
|
|

07-17-2008, 05:55 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
|
|
|
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.
|
|

07-17-2008, 11:04 AM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 481
|
|
|
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).
|
|

07-17-2008, 01:22 PM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 31
|
|
|
The easiest way would be to place a synchronized block around the print statements with the synchronized object being System.out
|
|

07-17-2008, 05:34 PM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 3
|
|
|
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.
|
|

07-17-2008, 06:48 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
|
|
|
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.
|
|

07-17-2008, 07:02 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Southwest
Posts: 789
|
|
|
thread communication
Originally Posted by _tony
...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.
__________________
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
|
|

07-19-2008, 12:04 PM
|
|
Member
|
|
Join Date: Mar 2008
Posts: 3
|
|
|
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();
|
|

07-21-2008, 05:01 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Southwest
Posts: 789
|
|
|
redundant?
Originally Posted by peter_lawrey
....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.
__________________
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
|
|

07-21-2008, 10:18 AM
|
|
Member
|
|
Join Date: Mar 2008
Posts: 3
|
|
Originally Posted by Nicholas Jordan
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.
|
|

07-22-2008, 10:27 AM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 3
|
|
|
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++;
}
}
}
}
|
|

07-22-2008, 10:31 AM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 3
|
|
|
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
|
|

07-22-2008, 05:38 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Southwest
Posts: 789
|
|
|
deadlock
Originally Posted by JPRABHU
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:
synchronized(firstObject){
synchronized(secondObject){
and does that in two places in the code.
__________________
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
|
|

09-08-2008, 02:22 PM
|
|
Member
|
|
Join Date: Sep 2008
Posts: 3
|
|
|
hI
hI,
This is Biswanath
|
|

09-08-2008, 02:35 PM
|
|
Member
|
|
Join Date: Sep 2008
Posts: 3
|
|
|
jsp
Hi,
How can we connect one jsp page to multiple jsp page?
|
|

09-09-2008, 12:45 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Southwest
Posts: 789
|
|
|
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.
__________________
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
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|