• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

why one thread runs

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 439
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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());
}
}
}
 
kashif sohail
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 439
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
kashif sohail
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Listen. That's my theme music. That's how I know I'm a super hero. That, and this tiny ad told me:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic