"JAVA programming ideas" study notes: Chapter 21 (concurrent)

Contents
Java programming ideas (a) 1 to 4 chapters: Overview of
Java programming ideas (b) Chapter 5: initialization and cleanup
Java programming ideas (c) Chapter 6: Access
Section (d) of the 7 Java programming ideas: Complex with class
Java programming ideas (e) Chapter 8: polymorphism
Java programming ideas (six) Chapter 9: Interface
Java programming ideas (seven) Chapter 10: internal class
chapter (h) of the 11 Java programming ideas: holding the object
thinking in Java (nine) Chapter 12: abnormal
thinking in Java (x) Chapter 13: string
chapter (XI) of 14 Java programming ideas: type information
(xii) Chapter 15 Java programming ideas: generic
Java programming ideas (13) Chapter 16: arrays
Java programming ideas (xiv) Chapter 17: In-depth study of container
Java programming ideas (xv) Chapter 18: Java I / O system
Java programming ideas (XVI) 19 Chapter: enumeration
Java programming ideas (17) Chapter 20: Notes
Java programming ideas chapter (xviii) 21: concurrency

Chapter 20 Concurrency

Foreword

  • Sequential programming: that all things in the program at any time can only perform one step.
  • Concurrent Programming: program can be performed in parallel in the plurality of partial programs.

21.1 concurrent multi-faceted

Concurrent programming difficulties:

  • Concurrent problem to be solved with a plurality; there are a variety of ways to achieve concurrent;
  • Also, there is no clear mapping between the two.

21.1.1 faster execution

  • Concurrent Objective: to improve performance is usually run on a single processor;
  • Concurrent achieve 1: The most direct way is to use the operating system level processes;
  • Concurrent achieve 2: Java is used in a more traditional way: to provide support for the thread on the basis of the order of language.

Concurrency and parallelism difference:

  • Concurrent: processing a plurality of tasks in turn: in fact executed sequentially, cpu executes only one thread at any one time to a different form by scheduling thread allocation period, but looks as if a plurality of tasks are performed simultaneously;
  • Parallel: handle multiple tasks simultaneously: that is, multiple tasks at the same time at the same time;

21.1.2 improved code design

  • Preemptive threading mechanism: the end of the thread scheduling mechanism will periodically, switch context to another thread, thereby providing a time slice for each thread, so that each thread is assigned to a reasonable amount of time to drive its task.
  • Non-preemptive threading mechanism: how much time each thread can take up much CPU time on CPU. In this scheduling, execution may be a very long time so that all the threads of the CPU other thread "starvation." In the processor is idle, that is, when the process does not use the CPU, the system can allow other processes to temporarily use CPU. CPU-thread has control of the CPU only when it is released on their own initiative CPU, other threads can use CPU.
  • Select thread mechanism: Java uses a preemptive threading mechanism.

21.2 The basic thread mechanism

  • Concurrent programming: making our program can be divided into a number of separate, independent tasks running.
  • Multi-threading mechanism: by multithreading, these independent subtasks each thread of execution will drive;

21.2.1 defined task

  • Task: the task thread can drive, so you need a way to describe the task, which may be provided by the Runnable interface. To define a task, simply write and implement Runnable run () method, so that the task can execute your command.
  • Thread execution: When a class is derived from Runnable, it must have run () method, but this method is not so special. It does not produce any internal threading capabilities. To achieve threading behavior, you must explicitly a task attached to the thread.

21.2.2 Thread class (thread)

  • Task execution: the traditional way Runnable object into a work task is to submit it to the constructor of a Thread (as a parameter)

21.2.3 Use Executor

  • The Executor (effector): Java SE5 java.util.concurrent package defined in the actuator, as management Thread object to simplify concurrent programming. And task execution between the client and provides a layer of indirection;
  • Executor: allows you to manage asynchronous tasks without having to show the life cycle management of threads; very common scenario: a single Executor is used to create all the tasks & management systems.

 

Published 223 original articles · won praise 73 · views 960 000 +

Guess you like

Origin blog.csdn.net/cbk861110/article/details/104120458