Processes and Threads (two basic units of execution in concurrent programming:OS feature called time slicing)
1,process: a process has a self-contained execution environment(a complete,private set of basic run-time resouces:its own memory space).
mistakes:Processes are often seen as synonymous with programs or applications.however,a single application may in fact be a set of cooperating processes.IPC resouces--pipes and sockets--is to facilitate communication between processes.
IN Java: a single process in most implementations of the Java virtual machine. with the help of PocessBuilder object,we can create additional processes(Java/jdk1.6.0/docs/api/java/lang/ProcessBuilder.html).
2, Threads(lightweight processes)
Exist within a process,so share the process's resources,including memory and open files,so communication.
Multithreaded execution is an essential feature of the Java platform.
Main thread.
Thread Objects(Each thread is associated with an instance of the class Thread.)
Two stategies for using Thread objects to create a concurrent application.
!to directly control thread creation and management,simply instantiate Thread each time the application needs to initiate an asynchronous task(discusses in this section).
!to abstact thread management from the rest of your application,pass the application's tasks to an executor(later discussed).
Defining and Starting a Thread
Two ways:
!Provide a Runnable object.the Runnable interface defines a single method,run,meant to contain the code executed in the thread.the Runnalbe object is passed to the Thread constructor,as in the HelloRunnable example:
/*public class HelloRunnable implements Runnable {
public void run() {
System.out.println("Hello from a thread!");
}
public static void main(String args[]) {
(new Thread(new HelloRunnable())).start(); //finally,there is a Thread created!!!!! and then the start method is invoked.and what's the difference between this and the javax.swing.Utilites.invokeLater()???
and the constuctor of Thread?????"Thread(Runnable target)
Allocates a new Thread object."
}
}*/
!Subclass Thread. an application can subclass Thread,providing its own implementation of run,as in the HelloThread example:
public class HelloThread extends Thread {
public void run() {
System.out.println("Hello from a thread!");
}
public static void main(String args[]) {
(new HelloThread()).start();
}
}
public class HelloThread extends Thread {
public void run() {
System.out.println("Hello from a thread!");
}
public static void main(String args[]) {
(new HelloThread()).start();
}
}
We'll examine some of these methods in the following sections.
Pausing Execution with Sleep(causes the current thread to suspend execution for a specified period.)
can be used
!for pacing,as shown in the examle that follows(SleepMessages???)
!and for waiting for another thread with duties that are understood to have time requirements,as with the SimleThreads example in a later section.
two overloaded versions of sleep are provided(millisecond and nanosecond)
!however,these sleep times are not guaranteed to be precise,because they are limited by the facilites provided by the underlying OS.
!also,the sleep period can be terminated by interrupts,as we'll see in a later section.so,in any case,you cannot assume that invoking sleep will suspend the thread for precisely the time period specified.
public class SleepMessages {
public static void main(String args[]) throws InterruptedException {
String importantInfo[] = {
"Mares eat oats",
"Does eat oats",
"Little lambs eat ivy",
"A kid will eat ivy too"
};
for (int i = 0; i < importantInfo.length; i++) {
//Pause for 4 seconds
Thread.sleep(4000);
//Print a message
System.out.println(importantInfo[i]);
}
}
}
Intertupts()