"JAVA thread Concurrency in Practice ': Concurrency Executor framework and tools

Foreword

Java threads and concurrency tools are an important part of application development, much attention of developers, also has some learning difficulties. "JAVA thread Concurrency in Practice" is for fast learning and practice guidelines Java 8 concurrent threads characteristics and tools.

"JAVA thread Concurrency in Practice" eight chapters, introduced the Thread class and Runnable interface , synchronization , wait and notifications , thread group , the timer framework , well development tools , synchronizer , lock frame , as well as advanced tools concurrent aspects, etc. Theme of. To the end of each chapter are exercises in a way, to help readers to consolidate what they have learned. Appendix A provides answers to all the exercises, Appendix B gives a tutorial on Swing thread.

This book suits to have some basic Java programmers learning to read, especially for readers to want to master the Java threads and concurrency tools to read reference.

Thread和Runnable

Java programs are executed by a thread, the thread has an independent execution path in the program. When multiple threads of execution, a path between them may be different from each other. For example, a thread may execute a case in a switch statement branch, another thread is likely to perform other case branches. Each application has a Java implementation of main () default main thread function. The application can also create time-intensive tasks in the background thread, in order to ensure that the response to the user. These packages execution code sequence runnable.Java thread object is called a virtual machine assigned to each thread a separate JVM stack space to avoid interfering with each other. Separate stacks so that the thread can track their own next instruction to be executed, these instructions will differ according to different threads. Stack space for each thread separately prepared - parts of method parameters, local variables, and copies the return value. Java is mainly through the java. Lang. Thread class and java. Lang. Runnable Interface

Thread and Runnable Profile

Thread class as the underlying operating system thread architecture provides a uniform interface (operating system is usually responsible for creating and managing threads). A single operating system and a Thread object associated with the thread.

Runnable interface to provide the code execution thread associated Thread object. The code in the void run Runnable () method, this method, although no no arguments and return values, but may throw.

1.1 Creating Thread and Runnable objects

In addition to the default main thread, the thread is entering the application by creating the appropriate Thread and Runnable objects. Thread class declares several constructor to initialize the Thread object. There are several construction methods needs to receive Runnable object as a parameter. We have to create Runnable objects in two ways. The first way is to create an anonymous class implements Runnable interface, as follows:

Runnable r m new Runnable()

{

@Override

public void run()

// perform some work

System. out . println( "Hello from thread");

}

};

Before Java8, this is the only way to create a runnable. However, Java 8 lambda expressions introduced more quickly and easily create runnable: Runnable r = () -> System out print1n ( "Hello from thread"); lambda is indeed more concise than anonymous classes... I will be in subsequent sections of this chapter and continue to use these language features.

1.2 Name of getting and setting thread

Each will be assigned a Thread object a name, it is a good debugging. If this name is not explicitly specified, the default will be a Thread- as a prefix. You can get this name by calling the Thread String getName () method. To set name, the name have to pass to an appropriate constructor function, such as Thread (Runnable r, String name), or the transfer of Thread void setName (String name) method. The following code fragment:

Thread t1 = new Thread(r, "thread t1"); .

System. out.println(t1.getName()); // Output: thread t1

Thread t2■new Thread(r);

t2. setName("thread t2");

System. out.println(t2. getName()); // Output: thread t2

1.3 acquire execution state of a thread

The execution state of the thread by Thread State enumeration constants identify:

● NEW: The state of the thread has not started.

● RUNNABLE: the next thread is JVM in this state.

● BLOCKED: The state thread is blocked waiting for a monitor lock (I will introduce listeners lock in Chapter 2).

● WAITING: a thread waits for another thread performs a specific operation in this state indefinitely.

● TIMED_ WAITING: in this state within a particular thread waits an additional period of time to perform an operation thread.

● TERMINATED: The state of the thread has exited.

Thread through the provision of Thread. State getState () method to let the application determine the current state of the thread. Examples are as follows:

Thread t = new Thread(r);

System. out. println(t.getState()); // output: NEW

Concurrency Executor framework and tools

2.1 Introduction to concurrent Tools

Java support for the underlying thread operation allows you to create multi-threaded applications. These procedures provide better performance and response capability than the corresponding single-threaded programs. Nevertheless, there remain a number of issues:

● low-level concurrency primitives such as synchronized and wait (), notify () are often difficult to use correctly. Misuse of these primitives used can cause race conditions, thread starvation, deadlocks, and other risks which are difficult to detect and debug.

● too dependent on synchronized primitives will bring performance issues that will affect the scalability of applications, such as for highly threaded applications like Web servers, the consequences will be very serious.

● developers often require advanced thread structure, such as thread pools and semaphores. Java support for the underlying thread operations are not included in these structures, the developers are forced to build a structure of their own, so that not only time-consuming and error-prone. To solve these problems, Java5 introduction of concurrency tools, this is a framework of robust and scalable high-performance threading tools classes, including thread pools and blocking queues. The framework consists of the following package more types.

●; java.util.concurrent: tools often use concurrent programming, such as: executors.

● java util concurrent.atomic:.. Supports and lock-free thread-safe operations on a single variable tools.

● java.util concurrent locks:.. On certain conditions (such objects will be suspended, "wait" until the thread wakes up another thread, this time some of the Boolean state of the object may become true) to acquire the lock and wait for execution Tools. For these types of locks than wait for the operator or by the Java-based synchronization and wait for the listener, wake-up mechanism having a better performance and scalability. This framework will also longnanoTime () method introduces class java.1ang.System, which allows you to do when you can measure the relative time nanosecond access time resources. Concurrent tools may be classified as executor, synchronizer (Synchronizer) and a lock frameworks. I will explore the executor in the following sections and discuss the remaining categories in the following sections.

Due to space limitations only show small series of partial directory contents welcome to join the exchange community for everyone to  click into the   available text document

 

 Threading API

Chapter One: Thread and Runnable
 

 

 Chapter II: Synchronization

 Chapter Three: Wait and notify

 Chapter 4: additional threading capabilities

Concurrent Tools

Chapter V: Concurrent Tools Eecutor Yao frame

 Chapter 7: lock frame

 

Published 66 original articles · won praise 48 · views 20000 +

Guess you like

Origin blog.csdn.net/kxkxyzyz/article/details/104228174