Concurrency(1)

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的调用。)
 
 

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下载 4使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、 4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值