100 likes | 265 Views
Threads. What is a thread?. A thread (also known as a thread of execution ) is a way to make a single program perform multiple tasks concurrently Various tasks within a single program can be made to run in parallel. When are threads used?.
E N D
Threads CSC220
What is a thread? • A thread (also known as a thread of execution) is a way to make a single program perform multiple tasks concurrently • Various tasks within a single program can be made to run in parallel CSC220
When are threads used? • Threads are typically used when a program consists of multiple tasks of varying priorities • It is typical to have a UI run in a high priority thread so that the user doesn’t sit around pushing mouse buttons and wondering what is going on • Lengthy computations are typically placed in low priority threads CSC220
Will multi-threaded programs run faster? • It depends on the underlying computer architecture and operating system • A multi-threaded application probably won’t run faster on a single core processor • Some tasks (like I/O) may be pushed off to external processors (video cards, etc.) • A multi-threaded application might run faster on a dual core processor if the operating system is designed to utilize both cores on a single program CSC220
Is it easy to write a multi-threaded application? • It depends on the supported provided by the programming language • Java provides the Thread API making it easy to write threads • Ada has multi-threading primitives built into the language • C/C++ are dependent on external libraries to provide multi-threading functionality CSC220
Is it easy to get a multi-threaded application working properly? • No! • It’s especially difficult when threads must • Share memory or other resources • Communicate with one another CSC220
Thread lifecycle Created Key: Italics are external events Block are Java Thread API calls start Ready scheduled by OS yield notify notifyAll interrupt interrupt resource available Running wait waiting on resource sleep task complete Waiting Blocked interrupt sleep expiration Asleep CSC220
Inherit from java.lang.Thread The run method holds the code Thread.stop() has been deprecated, do something like this instead. Don’t let one thread monopolize the CPU The specifics (Java API) public class ThreadClass extends Thread { private boolean go; public ThreadClass () { go = true; } public void Stop () { go = false; } public void run () { while (go) { // -- do something here try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } } } CSC220
Construct thread objects Start threads Stop threads Managing a thread public class TestThread { public static void main(String[] args) { ThreadClass thread1 = new ThreadClass(1); ThreadClass thread2 = new ThreadClass(2); ThreadClass thread3 = new ThreadClass(3); thread1.start(); thread2.start(); thread3.start(); // -- Do some other stuff in here // -- sets the loop control variable // (Thread.stop was declared unsafe) thread1.Stop(); thread2.Stop(); thread3.Stop(); } } CSC220
Sharing memory amongst threads • Exercise CSC220