Java Threads - Tutorial

Java Threads - Tutorial - Threads in Programming Languages...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Henri Casanova (henric@hawaii.edu) ICS491 - Spring 2007 Concurrent and High-Performance Programming Introduction to Java threads Threads in Programming Languages ! Several programming languages have provided constructs/abstractions for writing concurrent programs " Modula, Ada, etc. ! Java does it like it does everything else, by providing a Thread class " Note that in this class we use J2SE 1.5.0 ! You create a thread object ! Then you can start the thread Extending the Thread class ! To create a thread, you can extend the thread class and override its “run()” method class MyThread extends Thread { public void run() { . . . } . . . } myThread t = new MyThread(); Example public class MyThread extends Thread { public void run() { for (int i=0; i<10; i++) { System.out.println(“Hello world #“+i); } } . . . } myThread t = new MyThread();
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Spawning a thread ! To launch, or spawn , a thread, you just call the thread’s start() method ! WARNING : Don’t call the run() method directly to launch a thread " If you call the run() method directly, then you just call some method of some object, and the method executes ! Fine, but probably not what you want " The start() method, which you should not override, does all the thread launching ! It launches a thread that starts its execution by calling the run() method Example public class MyThread extends Thread { public void run() { for (int i=0; i<5; i++) { System.out.println(“Hello world #“+i); } } } public class MyProgram { public MyProgram() { MyThread t = new MyThread(); t.start(); } public static void main(String args[]) { MyProgram p = new MyProgram(); } } What happens ! The previous program runs as a process " Running inside the JVM ! In fact, the program runs as a single thread within a process ! When the start() method is called, the process creates a new thread ! We now have two threads " The “main”, “original” thread " The newly created thread ! Both threads are running " The main thread doesn’t do anything " The new thread prints messages to screen and exits ! When both threads are finished, then the process terminates Example ! The previous example wasn’t very interesting because the main thread did nothing " Admittedly, this example is not interesting because the program doesn’t do anything useful, but we’ll get there eventually ! In fact, we could have achieved the same result with no thread at all ! So, let’s have the main thread to something
Background image of page 2
Example public class myThread extends Thread { public void run() { for (int i=0; i<5; i++) System.out.println(“Hello world #“+i); } } public class MyProgram { public MyProgram() { MyThread t = new MyThread(); t.start(); for (int i=0; i<5; i++) System.out.println(“foo”);
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 10

Java Threads - Tutorial - Threads in Programming Languages...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online