GOALS FOR TODAY o THREADING ISSUES
GOALS FOR TODAY THREADING ISSUES
THREAD LIBRARIES o POSIX Pthreads Either a user-or kernel-level library o Win32 Kernel-level library o Java Implemented using a thread library available on the host system
THREAD LIBRARIES POSIX Pthreads Either a user- or kernel-level library Win32 Kernel-level library Java Implemented using a thread library available on the host system
JJAVA THREADS o Java threads are managed by the JVM o Java threads may be created by: Implementing the Runnable interface public interface Runnable public abstract void run();
JAVA THREADS Java threads are managed by the JVM Java threads may be created by: Implementing the Runnable interface
JAVA THREADS-EXAMPLE PROGRAM class MutableInteger { private int value; public int getValue(){ return value; public void setValue(int value){ this.value value; } class Summation implements Runnable private int upper; private MutableInteger sumValue; public Summation(int upper,MutableInteger sumValue){ this.upper upper; this.sumValue sumValue; public void run(){ int sum =0; for (int i =0;i <upper;i++) 8um+=1; sumValue.setValue(sum);
JAVA THREADS - EXAMPLE PROGRAM
JAVA THREADS-EXAMPLE PROGRAM public class Driver { public static void main(String[]args){ if (args.length 0){ if (Integer.parseInt(args [O])<0) System.err.println(args [O]+must be >0."); else //create the object to be shared MutableInteger sum new MutableInteger(); int upper Integer.parseInt (args [O]); Thread thrd =new Thread(new Summation(upper,sum)); thrd.start(); try thrd.join(); System.out.println ("The sum of "+upper+"is "+sum.getValue()) } catch (InterruptedException ie){} else System.err.println("Usage:Summation <integer value>"); }
JAVA THREADS - EXAMPLE PROGRAM