Concurrency(1)

来源:互联网 发布:查询价格的软件 编辑:程序博客网 时间:2024/06/06 20:04

Lesson: Concurrency
 Software that can do such things is known as concurrent software:Even the word processor should always be ready to respond to keyboard and mouse events, no matter how busy it is reformatting text or updating the display.
  Processes and Threads
Processing time for a single core is shared among processes and threads through an OS feature called time slicing.
 Processes
A process has a self-contained execution environment. A process generally has a complete, private set of basic run-time resources; in particular, each process has its own memory space.
 Threads
Threads exist within a process — every process has at least one. Threads share the process's resources, including memory and open files. This makes for efficient, but potentially problematic, communication.
 Thread Objects
Each thread is associated with an instance of the class Thread. There are two basic strategies 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.
To abstract thread management from the rest of your application, pass the application's tasks to an executor.
 Defining and Starting a Thread
An application that creats an instance of Thread must provide the code that will run in that thread.
there are two ways to do this:
Provide a Runnable object.the Runnable interface defines a single method,run,meant to contain the code executed in the thread.the Runnable 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();
    }

}
Subclass Thread.
The Thread class itself implements Runnable, though its run method does nothing. 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();
    }

}
This lesson focuses on the first approach, which separates the Runnable task from the Thread object that executes the task. Not only is this approach more flexible, but it is applicable to the high-level thread management APIs covered later.

Pausing Execution with Sleep
 Also, the sleep period can be terminated by interrupts
Interrupts
A thread sends an interupt by invoking interrupt on the Thread object for the thread to be interrupted. For the interrupt mechanism to work correctly, the interrupted thread must support its own interruption.
Supporting Interruption
 how does a thread support its own interruption?(its own interruption???? what it is????怎么叫“它自己的中断”?这个中断是为别的线程中断自己而准备,还是当接受到中断命令后自己再中断???)
 If the thread is frequently invoking methods that throw InterruptedException, it simply returns from the run method after it catches that exception.(要是不频繁呢?或者说频繁到什么程度才称的上频繁?)
 What if a thread goes a long time without invoking a method that throws InterruptedException?
The Interrupt Status Flag
using an internal flag.
 methods invoked:Thread.interrupt,Thread.interrupted,Thread.isInterrupted
 By convention, any method that exits by throwing an InterruptedException clears interrupt status when it does so. However, it's always possible that interrupt status will immediately be set again, by another thread invoking interrupt.
 Joins
The join method allows one thread to wait for the completion of another. (这里面的one与another的关系?谁又是这个方法的调用者呢?)
 If t is a Thread object whose thread is currently executing,
t.join();
causes the current thread to pause execution until t's thread terminates. (这不是加塞么????你在一边候着,我先用,用完后你才能用!!!!)
 Like sleep, join responds to an interrupt by exiting with an InterruptedException.(也就是说join也是可能被打断的!!!!)
The SimpleThreads Example
 The main thread creates a new thread from the Runnable object, MessageLoop, and waits for it to finish. If the MessageLoop thread takes too long to finish, the main thread interrupts it.(针对此句,重点看观察下面的MessageLoop的Runnable特性和main thread对interrupts的调用。)
 
 

原创粉丝点击