COP 4338 Lecture 5: Multithreading
Document Summary
Threads= prog unit that is executed independently of other parts of the prgrm. Java virtual machine executes each thread in the prgrm for short amount of time. Gives impression of parallel execution (not actually parallel). Runnable r = new myrunnable(): construct a thread obj from the runnable obj thread t = new thread (r), call start method to start the thread: t. start(); Greetingrunnable class: public class greetingrunnable implements runnable private string greeting; public greetingrunnable (string agreeting) greeting = agreeting; public void run() Need to loop 10 times through task actions: >to get date and time construct date obj: >to wait a second use the sleep method of the thread class. >sleeping thread can generate an interruptedexception (try catch) Terminate the thread run method(3) public void run() try. 1st construct an obj of your runnable class: Then construct a thread and call its start method: > runs each thread for a short amount of time (time slice)