File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads 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 "Threads" Watch "Threads" New topic
Author

Threads

Tanuja Vaid
Ranch Hand

Joined: Mar 07, 2002
Posts: 51
public class TestClass implements Runnable
{
int x = 0, y = 0;
public void run()
{
while(true)
{
x++; y++;
System.out.println(" x = "+x+" , y = "+y);
}
}
public static void main(String[] args)
{
TestClass tc = new TestClass();//1
new Thread(tc).start();//2
new Thread(tc).start();//3
}
}
will line 1 also create one thread.So in all how many threads will be created 2 or 3.
John Williamer
Greenhorn

Joined: Mar 27, 2002
Posts: 23
Only two threads will be created as all you have done is create an object in the line that you think might have created the first thread
Tanuja Vaid
Ranch Hand

Joined: Mar 07, 2002
Posts: 51
watch this code.....
public class TestClass implements Runnable
{
int x = 5;
public void run()
{
this.x = 10;
}
public static void main(String[] args)
{
TestClass tc = new TestClass();
new Thread(tc).start(); // 1
System.out.println(tc.x);
}
}
What will it print when run?
Ans:The output can't be determined
The jq+ says:
Note that, when you create and start a new Thread (at line 1) there are two threads running. (The main() thread and the thread that you just started!). Both the threads are trying to use the same variable. Now, which thread will run first cannot be determined so whether the main() thread reads 'x' first or the new thread changes 'x' first is not known. So the output of the program cannot be determined.
NOW WHERE AM I GOING WRONG???I AM CONFUSED
Tanuja Vaid
Ranch Hand

Joined: Mar 07, 2002
Posts: 51
Hey val,
if you are around,pls give your input.
If possibe can you pls explain the difference in thread creation between the first code and the second code.
TIA
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Remember, there is a thread running that is executing your application. This thread is often known as the main thread and executes, oddly enough, your main method. This thread is started by the JVM when you run your application.
Now, in Line 1 of your example, you're launching a new user thread that will begin executing the run() method of the class TestClass.
Now, just after you start that thread, the main thread tries to output the value of tc.x. However, the new thread that you just started is trying to modify that value. So which happens first? Does the value get changed by your thread or does it get printed by the main thread? Obviously, the order in which these operations occur will impact the final output of this application.
The answer is: we don't know. How threads are handled and how context switching is done is JVM and operating system dependent. How this application runs on a Windows machine might be different that on UNIX and might still be different than on a Macintosh. Becuase context switching is handled differently on different systems, we can't possibly guarantee what the result of running this application might be. It might even produce different results from one execution to the next on the same machine.
I hope that helps,
Corey
[ March 27, 2002: Message edited by: Corey McGlone ]

SCJP Tipline, etc.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Tanuja Vaid:
If possibe can you pls explain the difference in thread creation between the first code and the second code.

There is no difference in the way threads are created between the first example and the second example (except for the number of threads created). Remeber, the JVM starts a main thread and then you're free to create any additional threads. In the first example, you create two threads of your own and in the second example, you only create one.
Corey
John Williamer
Greenhorn

Joined: Mar 27, 2002
Posts: 23
class TestClass implements Runnable
{
int x = 5;
public void run()
{
this.x = 10;
}
public static void main(String[] args)
{
TestClass tc = new TestClass();
new Thread(tc).start(); // 1
for (int a=0;a<20;a++)
System.out.println(tc.x);
}
}
try that. What was happening was when the thread was put into the running state the program had already printed the content of x. All this does is slow down the program a little by repeating to print tc.x and giving the thread a chance to run.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by John Williamer:


try that. What was happening was when the thread was put into the running state the program had already printed the content of x. All this does is slow down the program a little by repeating to print tc.x and giving the thread a chance to run.

A more elegant approach might be to do something like this:

The call to Thread.sleep causes the main thread to be removed from the running state and added to the sleeping state. This will also cause this thread to give up the processor so that another thread, which is in the ready-to-run state, can use it. Most likely, you'll see two different values printed for tc.x, but that still is not guaranteed.
Corey
J Hreich
Ranch Hand

Joined: Mar 22, 2002
Posts: 37
the start() method returns right away. So in the original piece of code, it will most likely print
the value of the variable before the thread
started acting on it. Of course, unless a context
switch happend right after the start() method
returned.
An interesting thing to experiment with is to add
a call to join() right after start(). A good
example for pausing until a thread finishes.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threads