ThreadBasics - Thread basics YHL Friday Threads 1 Java Thread class YourClass extends Thread cfw public void run cfw code for the thread i.e what

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

View Full Document Right Arrow Icon
YHL Threads 1 Thread basics Friday, October 21, 2011
Background image of page 1

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

View Full DocumentRight Arrow Icon
YHL Threads 2 Java Thread class YourClass extends Thread { public void run () { // code for the thread, i.e. what it does } } ... public static void main(String [] args) { YourClass t1 = new YourClass(". .."); t 1 .start (); } main start Friday, October 21, 2011
Background image of page 2
YHL Threads 4 Thread Execution Time t1 t1 t1 t2 t2 t2 t3 t3 t3 t1 t1 t1 t1 t1 t1 t2 t2 t2 t2 t2 t2 t3 t3 t3 t3 t3 t3 time One core or processor > 2 cores or processors Friday, October 21, 2011
Background image of page 3

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

View Full DocumentRight Arrow Icon
YHL Threads 5 Many Threads, Few Processors advantages of many threads, even on single processor – impression of continuous progress in all threads – improve utilization of different components – handle user inputs more quickly disadvantage of many threads – add slight work to program structure – incur overhead in thread creation – cause complex interleaving the execution and possibly wrong results (if you do not think "in parallel") Friday, October 21, 2011
Background image of page 4
5 A typical program has sequential periods of execution followed by parallel periods followed by parallel periods followed by sequential periods Friday, October 21, 2011
Background image of page 5

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

View Full DocumentRight Arrow Icon
6 As programmers, we can spawn new threads at the start of a parallel phase, and kill them at the end of the phase Or we can start the threads once, and at the end of a parallel period put them into a pool to be reused at the next parallel period Or have them suspend to begin working again Friday, October 21, 2011
Background image of page 6
Two ways to spawn threads in Java Way 1 Inherit from the Thread class Invoke the run method on the object The run method can be viewed as the " main " method of the thread 7 public class myThread extends Thread { . . . public void run( ) { // thread actions here } . . . } myThread t1 = new Mythread(. ..); t1.start( ); // invokes t1.run() Friday, October 21, 2011
Background image of page 7

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

View Full DocumentRight Arrow Icon
Two ways to spawn threads in Java Way 1 Implement Runnable interface Invoke the start method on the Thread object The run method can be viewed as the " main " method of the thread 8 public class myThread extends C implements runnable { public myClass( ) { Thread t = new Thread(this); t.start( ); public void run( ) { // thread actions here } . . . } myThread t1 = new Mythread(. ..); t1.start( ); Friday, October 21, 2011
Background image of page 8
ordering and atomicity are important a = b.getBalance( ); a++; b.setBalance(a); a = b.getBalance( ); a++; b.setBalance(a); Thread 0 Thread 1 Program Memory object b $497 balance Both threads can access the same object Thread 0 a Thread 1 a Friday, October 21, 2011
Background image of page 9

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

View Full DocumentRight Arrow Icon
a = b.getBalance( ); a++; b.setBalance(a); a = b.getBalance( ); a++; b.setBalance(a); Thread 0 Thread 1 Program Memory object b $497 balance Thread 0 a $497 Thread 1 a Friday, October 21, 2011
Background image of page 10
a = b.getBalance( ); a++; b.setBalance(a); a = b.getBalance( ); a++; b.setBalance(a); Thread 0 Thread 1 Program Memory object b $497 balance Thread 0 a $497 Thread 1 a $497 Friday, October 21, 2011
Background image of page 11

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

View Full DocumentRight Arrow Icon
a = b.getBalance( ); a++; b.setBalance(a); a = b.getBalance( ); a++; b.setBalance(a); Thread 0 Thread 1 Program Memory object b $498 balance Thread 0 a $498 Thread 1 a $497 Friday, October 21, 2011
Background image of page 12
a = b.getBalance( );
Background image of page 13

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

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

This note was uploaded on 02/19/2012 for the course ECE 462 taught by Professor Samuelmidkiff during the Fall '11 term at Purdue University-West Lafayette.

Page1 / 49

ThreadBasics - Thread basics YHL Friday Threads 1 Java Thread class YourClass extends Thread cfw public void run cfw code for the thread i.e what

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

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