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 why one thread runs Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
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
Val Dra
Ranch Hand

Joined: Jan 26, 2001
Posts: 439
i changed your code a little so infact thread 2 and i both do run look at this
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 (){
while(true)
{//line 1
x++;
y++;
System.out.println("x"+x+"y"+y+"CurrentThread"+Thread.currentThread().getName());
}
}
}

Val SCJP <BR>going for SCJD
kashif sohail
Greenhorn

Joined: Dec 04, 2000
Posts: 29
actually my question is why 2nd thread did not runs (as it shows)
and would u plz explain the use of while
thanx
kashif
Val Dra
Ranch Hand

Joined: Jan 26, 2001
Posts: 439
i tell you 2nd threads runs as well , if you look at it , try to run the example that i modified 2nd threads does run infact. The reason why i switched to while is to see longer which threads can run becouse in your loop the thread might not be run because we don't know when it can run. So i changed it , to while ,Almost always you will have a while loop or endless for loop
Aftab Ahmed
Ranch Hand

Joined: Nov 22, 2000
Posts: 42
Hi Sohail,
i just little change your code
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"+currentThread().getName()); //here is changes just add currentThread() method
}
}
}
hope that help
Aftab

Keep Simlling
kashif sohail
Greenhorn

Joined: Dec 04, 2000
Posts: 29
tnax all but what about my second question
how to sync the method without usin sysncronized key word in mehtod definition (actulyy sync the some block of method)
2:
what if i wish to synchronize it like
synchronize(this){ //after line 1
but it gives me error
any help
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why one thread runs
 
Similar Threads
I am new to Java programming
why one thread runs
confuse on the answer