This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Threading question  RSS feed

 
Ken Teoh
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I have a threading question. which methods do you add to the following
class so that the output is
run
run
doIt ?

Also is calling a.run() the same as a.start()? It seems they do the same thing

Thanks

public class AddString extends Thread{
public void run() {
System.out.println("run");
}
public void doIt() {
System.out.println("doIt");
}
public static void main(String[] argc) throws InterruptedException {
AddString a = new AddString();
a.start();
// Add code here
}
}
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also is calling a.run() the same as a.start()? It seems they do the same thing


When you call a.run(), it is just calling the method -- just like calling any other method.

When you call a.start(), it is starting a new thread in the system. This new thread, is scheduled with the other threads. The current thread returns from start(), and continues execution. While this new thread will run starting with the run() method.

Henry
 
Ken Teoh
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I managed to solve the problem

Apparently to print out

run,run,doIt

you need to call the methods start,run,join and lastly doIt.
However, what I don't understand why the method join is necessary
as there is only one thread in existance. Can someone explain why this
works?

Thanks

public class AddString extends Thread{
public void run() {
System.out.println("run");
}
public void doIt() {
System.out.println("doIt");
}
public static void main(String[] argc) throws InterruptedException {
AddString a = new AddString();
a.start();
// code starts here
a.run();
a.join();
a.doIt();

}
}
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you need to call the methods start,run,join and lastly doIt.
However, what I don't understand why the method join is necessary
as there is only one thread in existance. Can someone explain why this
works?


There are two threads -- the main thread and the thread that the main thread started when it called the start() method. When the main thread called join(), it is basically waiting until the other thread finishes.

BTW, I am assuming this is only for testing purposes, as this would be a silly homework problem.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!