Results 1 to 20 of 21
Thread: Java Threads Interview Question
- 07-16-2008, 09:11 PM #1
Member
- Join Date
- Jul 2008
- Posts
- 3
- Rep Power
- 0
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, 09:14 PM #2
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
- 07-16-2008, 09:20 PM #3
Member
- Join Date
- Jul 2008
- Posts
- 3
- Rep Power
- 0
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-16-2008, 11:35 PM #4
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, 12:13 AM #5
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
- 07-17-2008, 03:55 AM #6
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, 09:04 AM #7
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
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, 11:22 AM #8
Member
- Join Date
- Jul 2008
- Posts
- 31
- Rep Power
- 0
The easiest way would be to place a synchronized block around the print statements with the synchronized object being System.out
- 07-17-2008, 03:34 PM #9
Member
- Join Date
- Jul 2008
- Posts
- 3
- Rep Power
- 0
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, 04:48 PM #10
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, 05:02 PM #11
thread communication
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
- 07-19-2008, 10:04 AM #12
Member
- Join Date
- Mar 2008
- Posts
- 10
- Rep Power
- 0
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, 03:01 AM #13
redundant?
Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
- 07-21-2008, 08:18 AM #14
Member
- Join Date
- Mar 2008
- Posts
- 10
- Rep Power
- 0
- 07-22-2008, 08:27 AM #15
Member
- Join Date
- Jul 2008
- Posts
- 3
- Rep Power
- 0
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, 08:31 AM #16
Member
- Join Date
- Jul 2008
- Posts
- 3
- Rep Power
- 0
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, 03:38 PM #17
deadlock
Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
- 09-08-2008, 12:22 PM #18
Member
- Join Date
- Sep 2008
- Posts
- 3
- Rep Power
- 0
hI
hI,
This is Biswanath
- 09-08-2008, 12:35 PM #19
Member
- Join Date
- Sep 2008
- Posts
- 3
- Rep Power
- 0
jsp
Hi,
How can we connect one jsp page to multiple jsp page?
- 09-08-2008, 10:45 PM #20
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
Similar Threads
-
threads question
By sandor in forum Threads and SynchronizationReplies: 9Last Post: 02-07-2009, 08:57 PM -
Job Interview About J2EE 2007
By pazuvn in forum Advanced JavaReplies: 1Last Post: 05-14-2008, 02:45 AM -
We need Java Programmers to interview
By jayson_sabale in forum Forum LobbyReplies: 4Last Post: 05-05-2008, 09:34 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks