![if !IE]> <![endif]>
Thread libraries provide programmers with an API for creating and managing threads.
Thread libraries may be implemented either in user space or in kernel space. The former involves API functions implemented solely within user space, with no kernel support. The latter involves system calls, and requires a kernel with thread library support.
There are three main thread libraries in use today:
1.POSIX Pthreads - may be provided as either a user or kernel library, as an extension to the POSIX standard.
2. Win32 threads - provided as a kernel-level library on Windows systems.
3. Java threads - Since Java generally runs on a Java Virtual Machine, the
implementation of threads is based upon whatever OS and hardware the JVM is running on, i.e. either Pthreads or Win32 threads depending on the system.
ü The following sections will demonstrate the use of threads in all three systems for calculating the sum of integers from 0 to N in a separate thread, and storing the result in a variable "sum".
The POSIX standard ( IEEE 1003.1c ) defines the specification for pThreads, not the implementation.
ü pThreads are available on Solaris, Linux, Mac OSX, Tru64, and via public domain shareware for Windows.
ü Global variables are shared amongst all threads.
ü One thread can wait for the others to rejoin before continuing.
ü pThreads begin execution in a specified function, in this example the runner( ) function:
2. Java Threads
ü ALL Java programs use Threads - even "common" single-threaded ones.
ü The creation of new Threads requires Objects that implement the Runnable Interface, which means they contain a method "public void run( )" . Any descendant of the Thread class will naturally contain such a method. ( In practice the run( ) method must be overridden / provided for the thread to have any practical functionality. )
ü Creating a Thread Object does not start the thread running - To do that the program must call the Thread's "start( )" method. Start( ) allocates and initializes memory for the Thread, and then calls the run( ) method. ( Programmers do not call run( ) directly. )
ü Because Java does not support global variables, Threads must be passed a reference to a shared Object in order to share data, in this example the "Sum" Object.
ü Note that the JVM runs on top of a native OS, and that the JVM specification does not specify what model to use for mapping Java threads to kernel threads. This decision is JVM implementation dependant, and may be one-to-one, many-to-many, or many to one.. ( On a UNIX system the JVM normally uses PThreads and on a Windows system it normally uses windows threads. )
3. Windows Threads
Similar to pThreads. Examine the code example to see the differences, which are mostly syntactic & nomenclature:
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.