Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

run()  RSS feed

 
Ram Pathan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can i make the main method return the value of i as 10 (i.e value modified in run method)
interface Hithere
{
public void hello();
}
public class test implements Runnable, Hithere
{
static Thread th;
int i;
public test()
{
System.out.println("Before run");
(new Thread("add")).start();
System.out.println("after run");
}
public static void main(String[] args)
{
t=new test();
System.out.println(t.i);
}
public void run()
{
i=10;
System.out.println("i am in run");
}
public void hello()
{
}
}
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to wait for the thread to finish executing before printing "t.i", you will need to join the thread.
You have a "static Thread th" in the "test" class, which you never use. Remove the "static", and assign to this variable when you do a "new Thread". That way, you can join it later.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!