|
|||||||||||||||||||||
|
|||||||||||||||||||||
|
|||||||||||||||||||||
Write code to define, instantiate and start new threads using both java.lang.Thread and java.lang.Runnable | |||||||||||||||||||||
|
|||||||||||||||||||||
Threads are lightweight processes that appear to run in parallel with your main program. Unlike a process a thread
shares memory and data with the rest of the program. The word thread is a contraction of "thread of execution",
you might like to imagine a rope from which you have frayed the end and taken one thread. It is still part of the
main rope, but it can be separated from the main and manipulated on its own. An example of where threads can be
useful is in printing. When you click on a print button you probably don't want the main program to stop responding
until printing has finished. What would be nice is that the printing process started running "in the background"
and allowed you to continue using the main portion of the program. It would also be useful if the main program would respond if the printing thread encountered a problem. A common example used to illustrate threads is to create a GUI application that launches a bouncing ball every time a button is clicked. Unlike most language threading is embedded at the heart of the Java language, much of it at the level of the ultimate ancestor class called Object. |
|||||||||||||||||||||
|
|||||||||||||||||||||
Of the two methods of creating a new thread the use of Runnable is probably more common, but you must know about both for the purpose of the exam. Here is an example of a class created with the Runnable interface. class MyClass implements Runnable{ public void run(){//Blank Body} } Creating the thread of execution. MyClass mc = new MyClass();
To do anything useful when you create a thread of execution from a class you would, of course need to put something where I have put //Blank Body. The other method for creating a thread is to create a class that is descended from Thread. This is easy to do but it means you cannot inherit from any other class, as Java only supports single inheritance. Thus if you are creating a Button you cannot add threading via this method because a Button inherits from the AWT Button class and that uses your one shot at inheritance. There is some debate as to which way of creating a thread is more truly object oriented, but you do need to go into this for the purpose of the exam. |
|||||||||||||||||||||
|
|||||||||||||||||||||
Although the code that runs in your thread is in a method called run, you do not call this method directly, instead you call the start method of the thread class. The Runnable interface does not contain a start method, so to get at this and the other useful methods for threads (sleep, suspend etc etc), you pass your class with the Runnable interface as the constructor to an instance of the Thread class. Thus to cause the thread to execute from a class that implements Runnable you would call the following MyClass mc = new MyClass(); Thread t = new Thread(mc); t.start(); |
|||||||||||||||||||||
|
|||||||||||||||||||||
Again note that was a call to start, not a call to run, even though it is the code in the run
method in your class that actually executes. If you create your class as a sub class of Thread you can simply call the start method. The drawback of sub classing the Thread class is that due to only supporting single inheritance you cannot inherit the functionality of any other class. |
|||||||||||||||||||||
|
|||||||||||||||||||||
![]() |
|||||||||||||||||||||
Question 1) What will happen when you attempt to compile and run this code? public class Runt implements Runnable{ public static void main(String argv[]){ Runt r = new Runt(); Thread t = new Thread(r); t.start(); } public void start(){ for(int i=0;i<100;i++) System.out.println(i); } } 1) Compilation and output of count from 0 to 99 Question 2) Which of the following statements are true? 1) Directly sub classing Thread gives you access to more functionality of the Java threading capability than
using the Runnable interface Question 3) What will happen when you attempt to compile and run the following code? public class Runt extends Thread{ public static void main(String argv[]){ Runt r = new Runt(); r.run(); } public void run(){ for(int i=0;i<100;i++) System.out.println(i); } } 1) Compilation and output of count from 0 to 99 Question 4) Which of the following statements are true? 1) To implement threading in a program you must import the class java.io.Thread |
|||||||||||||||||||||
|
|||||||||||||||||||||
Answer 1) 3) Compile time error: class Runt is an abstract class. It can't be instantiated. The class implements Runnable but does not define the run method. Answer 2) 3) Both using the Runnable interface and subclassing of Thread require calling start to begin execution of a Thread 4) The Runnable interface requires only one method to be implemented, this is called run Answer 3) 1) Compilation and output of count from 0 to 99 However, note that this code does not start the execution of the Thread and the run method should not be called in this way. Answer 4) 2) The code that actually runs when you start a thread is placed in the run method 3) Threads may share data between one another 4) To start a Thread executing you call the start method and not the run method You do not need to import any classes as Threading is an integral part of the Java language |
|||||||||||||||||||||
|