A thread is the basic unit of CPU utilization within a process. Multithreaded processes provide benefits like increased responsiveness, resource sharing, and ability to utilize multiple processors. There are different models for how user threads created by libraries map to kernel threads managed by the OS kernel, including many-to-one, one-to-one, and many-to-many mappings. Popular thread libraries include Pthreads, Win32 threads, and Java threads which provide APIs for creating and managing threads.
21. 4.3.3 JAVA THREADS Threads are fundamental model of program execution in a Java program, and the Java language and it’s API provide a rich set of features for the creation and management of threads.
22. class Sum { private int sum; public int getSum() { return sum; } public void setSum (int sum) { this.sum = sum; } } class Summation implements Runnable { private int upper; private Sum sumValue; public Summation (int upper, Sum sumValue) { this.upper = upper; this.sumValue = sumValue } public void run() { int sum = 0; for (int i=0; i<= upper; i++) sum+=I; sumValue.setSum(sum); } }
23. public class Driver { public static void main(String[]args){ if(args.length >0){ if (Integer.parseInt (args[0])<0) System.err.printIn(args[0]+” must be >= 0.”); else{ Sum sumObject = new Sum(); int upper = Integer.parseInt(args[0]); Thread thrd = new Thread(new Summation (upper, sumObject)); thrd.start(); try { thrd.join(); System.out.printIn (“The sum of “+upper+” is “+sumObject.getSum()); } catch (InterruptedException ie) { } } } else System.err.println(“Usage: Summation <integer value>”); } }
24. 4.4.3 THREADING ISSUES In this section, we discuss some of the issues to consider with multithreaded programs.
25.
26.
27.
28.
29.
30. 4.4.4 THREAD POOLS Limits the number of threads that exist at anyone point. This is particularly important on systems that cannot support a large number of concurrent threads. Example: DWORD WINAPI PoolFunction (AVOID Param) { /** * this function runs as a separate thread. **/ }
31.
32. 4.4.5 THREAD-SPECIFIC DATA In a transaction-processing system, we might service each transaction in a separate thread. User thread Lightweight process Kernel thread Figure 4.9 Lightweight process (LWP) LWP k
33.
34.
35.
36.
37.
38. Some of these flags are listed below: The set of open file is shared. CLONE_FILES Signal handlers are shared. CLONE_SIGHAND The same memory space is shared. CLONE_VM File-system information is shared. CLONE_FS MEANING FLAG