File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes why one thread runs Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "why one thread runs " Watch "why one thread runs " New topic
Author

why one thread runs

kashif sohail
Greenhorn

Joined: Dec 04, 2000
Posts: 29
public class ethread extends Thread
{ int x;
int y;
public static void main(String []args)
{
ethread et=new ethread();
Thread a=new Thread(et);
Thread b=new Thread(et);
a.start();
b.start();
}
public void run (){
for (int i=0;i<10;i++)
{//line 1
x++;
y++;
System.out.println("x"+x+"y"+y+"CurrentThread"+getName());
}
}
}
the reuslt is like blew what i m not sure is where is the second thread
2:
what if i wish to synchronize it like
synchronize(this){ //after line 1
but it gives me error
any help

x1y1CurrentThreadThread-0
x2y2CurrentThreadThread-0
x3y3CurrentThreadThread-0
x4y4CurrentThreadThread-0
x5y5CurrentThreadThread-0
x6y6CurrentThreadThread-0
x7y7CurrentThreadThread-0
x8y8CurrentThreadThread-0
x9y9CurrentThreadThread-0
x10y10CurrentThreadThread-0
Ben Roy
Ranch Hand

Joined: Nov 01, 2000
Posts: 70
I'm by no means an expert on threads, but from the little I have done it looks like you might have a fundamental flaw in what you are trying to do. Why are you doing:
ethread et=new ethread();
Thread a=new Thread(et);
Thread b=new Thread(et);
instead of just doing:
ethread a=new ethread();
ethread b=new ethread();
Try that and see if you get the result you are looking for.
Ben Roy
Ranch Hand

Joined: Nov 01, 2000
Posts: 70
Oh yeah, and I think you should probably put a Thread.sleep() inside your for loop so that it releases control back to the system and other threads can take a turn.
kashif sohail
Greenhorn

Joined: Dec 04, 2000
Posts: 29
actually i m doing this to pass one object to the 2 different threads that's why i did it in this way
any more help plz
Ben Roy
Ranch Hand

Joined: Nov 01, 2000
Posts: 70
To clarify a bit more what I was saying, I believe that the behavior you are seeing is exactly what one should expect. You are only creating one ethread, et, which has a ThreadName of 0 (since you don't set one explicitly). Then you pass the run object of that thread in to both of your new threads and tell them both to execute it. But since only one instance of et actually exists (passing by reference), they are both attempting to run the same runnable object. I'm not entirely sure if they are actually both running it or if one is being blocked, but the end result is the same which is that the run object of et is being executed. If think about getName() from the perspective of run(), the thread that it exists within is et. When it runs, it's printing the name of et.
If you are wanting to pass an object to two threads and have them run concurrently, I think you need to change your code a bit. Create the object as something other than a Thread or Runnable, and then pass it to two instances of your ethread object.
Satya Narayana
Greenhorn

Joined: Nov 08, 2000
Posts: 9
x1y1CurrentThreadThread-0
x2y2CurrentThreadThread-0
x3y3CurrentThreadThread-0
x4y4CurrentThreadThread-0
x5y5CurrentThreadThread-0
x6y6CurrentThreadThread-0
x7y7CurrentThreadThread-0
x8y8CurrentThreadThread-0
x9y9CurrentThreadThread-0
x10y10CurrentThreadThread-0
x11y11CurrentThreadThread-0
x12y12CurrentThreadThread-0
x13y13CurrentThreadThread-0
x14y14CurrentThreadThread-0
x15y15CurrentThreadThread-0
x16y16CurrentThreadThread-0
x17y17CurrentThreadThread-0
x18y18CurrentThreadThread-0
x19y19CurrentThreadThread-0
x20y20CurrentThreadThread-0
This is the output I got for your program. It means two threads are running. But you are getting same thread name as "Thread-0". Because you are using same object. If you want different number for every thread running do the following things
1. add one static class variable
static int kount=0;
2.Add one new method
private void synchronized getNewThread()
{
return (kount++);
}
3. Add this line at the begining of run() method.
int k=getNewThreadName();
4. Use this integer k inplace of getName. Then you can get a different name for every thread.
I'm not getting where you used synchronized(this)( line 1 means ???)
If you use this in main method definetly it will give error that nonstatic variable this can not be accessed through static methods.
In any call level methods it will work fine.
------------------
Rahul Mahindrakar
Ranch Hand

Joined: Jul 28, 2000
Posts: 1859
Check out a similar discussion Here
George Toronto
Ranch Hand

Joined: Dec 30, 2000
Posts: 78
Hi, friends
there is a review about the thread chain.
Plz check the Here
Tanveer Rameez
Ranch Hand

Joined: Dec 11, 2000
Posts: 158
hi
To know which thread is running, write a cinstructor of ethread with a call to its super class constructor passing the name of the thread.
[code]
public clas ethread extends Thread{
int x,y;
ethread(String threadname)
{
super(threadname);
}
[code]
If you want to run two threads, simply instantiate two ethread objects, since ethread is also a thread as it extends Thread.Then invoke their start method(inherited from Thread),which will invoke the overloaded run method.
[code]
public static void main(String []args)
{
ethread e1=new ethread("thread 1");
ethread e2=new ethread("thread 2");
e1.start();
e2.start();
}
public void run()
{
for (int i=0;i<10;i++)
{//line 1
x++;
y++;
System.out.println("x"+x+"y"+y+"CurrentThread"+getName());
}
}
[code]
hope this simple explaination helps you
regards
Tanveer


Author of JPhotoBrush Pro (www.jphotobrushpro.com)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why one thread runs