Java Threads - Tutorial

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

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

View Full Document Right Arrow Icon
Henri Casanova ([email protected]) 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();
Image of page 1

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

View Full Document Right 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
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”); } public static void main(String args[]) { MyProgram p = new MyProgram(); } } What happens?
Image of page 3

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern