wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread" Watch "Thread" New topic
Author

Thread

Richard Selva
Greenhorn

Joined: Apr 10, 2002
Posts: 15
Some Question I find in one interview regarding the threads give me solution
1.Write a program that illustrates the usage of synchronization. There are 3 threads each of which prints the string shown below through a synchronized method.

2.Create two threads. One of the threads has to print even numbers and another should print odd numbers.
thanks,
selva.
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

Richard-
Rather than simply giving you the solution..... how about you try it and we can make suggestions as to how to get your code to work better or how to overcome a problem?
We'd rather not do your homework for you but would be more than happy to answer specific questions.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Hmmm...rather than simply giving you the solution, how about if I point you in the right direction to find more information about threads? Give it a try and see what you come up with. If you have specific questions, please ask.
For more information, check out:
API Spec for Thread class
API Spec for Runnable Interface
JLS, §Threads and Locks
JLS, §8.4.3.6 synchronized Methods
JLS, §14.18 The synchronized Statement
JLS, §16.2.13 synchronized Statements
Sun's Tutorial
JavaWorld Article
That should be more than enough to get you started. Like I said, if you have specific questions while you're doing this, please ask.
Best of luck,
Corey


SCJP Tipline, etc.
Richard Selva
Greenhorn

Joined: Apr 10, 2002
Posts: 15
Help me here
class TestThread implements runnable
{
public static void main(String args[])
{
Thread odd = new Thread(this);
odd.start();
odd.sleep(500);
Thread even = new Thread(this);
even.start();
even.sleep(1000);
}
public void run()
{
/*
If(odd)
printOddnumbers()
else
printEventNumbers()
*/
Struggling here to find the correct thread
}
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
I think you might find it easier to use two unique Runnable classes rather than just one. If you were to do something along these lines:

In the above code, line 1 will cause the run method in the OddPrintingThread class to be invoked by a thread and line 2 will cause the run method in EvenPrintingThread to be invoked.
This way, you don't have to try to determine which set of numbers to print. Each thread is responsible for its own set.
Also, in your code, you have some code written like this:

Thread.sleep is a static method with the following description in the API Spec:

Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds.

So, in fact, odd.sleep(500) won't cuase the thread odd to sleep, it will cause the currently executing thread, the main thread, to sleep. So, even though I'm assuming you wanted to make the Thread referenced by odd sleep for 500 milliseconds, you're going to cause the main thread to sleep for 500 milliseconds and thereby make the processor available for the odd thread, allowing it to execute. That's exactly the opposite of what I'll bet you intended.
However, I see no need to use Thread.sleep() in this problem, at all.
Corey
 
Don't get me started about those stupid light bulbs.
 
subject: Thread
 
Similar Threads
synchronized run method.
How to get the Thread ID
Print Odd and even number in sequence with using two threads
Help with Assignment 1.5 - Times
Threads: overriding start()