• Post Reply Bookmark Topic Watch Topic
  • New Topic

java Thread  RSS feed

 
hari vallabh shukla
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In java class i have create one thread t1 by implementing Runnable interface and define it run() method,everything work fine but when i see that which thread is executing run() method then i have seen both thread ,first which i have created i.e t1 and second is main thrad i.e(public static void main(String str)).so both thread is executing same piece of code,so how can i stop main thread to executing same piece of code
 
Saif Asif
Ranch Hand
Posts: 440
Eclipse IDE Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings Hari,

Kindly share your code here so we can have a better understanding as to what is going on .
 
hari vallabh shukla
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Below code which i have made for thread in which i have doubt

public class demoClass implements Runnable {

static Thread t1;
static int i=0;


static demoClass democlass=new demoClass();
public static void main (String str[])
{


t1=new Thread(demoClass.democlass,"t1");

t1.start();

t1.run();


System.out.println("iam inside main method ");


}
public void run()
{



i++;
System.out.println(Thread.currentThread().getName()+"*************************** inside run() method "+i);


}
}
 
Saif Asif
Ranch Hand
Posts: 440
Eclipse IDE Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well first of all , I am unable to understand why you have declared a static thread inside your Runnable class ( what are you trying to achieve ) . Secondly you should understand the difference between the methods run() and start(). To initiate a thread , you use the start() method, NOT the run().

 
hari vallabh shukla
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks ASIF for correcting my idea, i thought that run() method is call by us explicitly but now i understand main method call implicitly run() method on different thread
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!