• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Threads

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Every plan is a little cooler if you have a blimp. And a tiny ad.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic