[Change] Multi-thread, have to say

Multi-threading thread: refers to the process of an implementation flow.
The difference between threads and processes: each process requires the operating system to assign a separate memory address space, and the same process all the threads in the same one address space to work, these threads can share the same piece of memory and system resources.

How do I create a thread?

There are two ways to create a thread, as follows:
1, extending java.lang.Thread class
2 to achieve Runnable interface
Thread class on behalf of the thread class, its two most important methods are:
run () - contains the thread to run code that is executed when the
Start () - used to start a thread

A thread can only be started one time. The second starts an exception will be thrown java.lang.IllegalThreadExcetpion

Inter-thread state conversion (as shown)

New Status: The new statement creates a new thread object is in the state, this time it like any other java object, only the memory is allocated in the heap ready state: When a thread is created after the other thread calls its start () method, which thread into the ready state. In view of this state to run the thread in the pool, waiting to get right to use the CPU to run the state: in this state of the thread take up CPU, the implementation of the code for the blocking state: When the thread is in blocking state, java virtual machine will not thread allocation CPU, until the thread re-enter the ready state, it is a chance to go running.
Blocking state is divided into three situations:
1, is located in the blocking state of the object to wait for the pool: When the thread is running, if you perform a certain object's wait () method, java virtual machine on the back to put the thread into the waiting pool of the object
2, located in the object lock in the blocking state, when the thread is running when trying to get an object's synchronization lock, if the object's synchronization lock has been occupied by the other threads, JVM will put this thread in this object The Suo pool.
3, the other blocking states: The current thread is executed sleep () method, or the calling of the other thread join () method, or issued by I / O request, it will enter the state.

The state of death: When the thread exits the run () method, entered the death state, the thread end of life cycle.
Or a normal exit
Or encounter an exception out of
Thread class isAlive () method to determine whether a thread is alive, when the thread is in the state of death or a new state, the method returns false, the rest of the state, the method returns true.

Thread scheduler thread scheduling model: time-sharing scheduling model and the preemptive scheduling model
JVM using preemptive scheduling model.
The so-called multi-threaded run concurrently, in fact, refers to a macro perspective, each thread in turn to obtain the right to use CPU, respectively, carrying out their respective mandates.
(Thread scheduling is not a cross-platform, it not only depends on the java virtual machine, it also depends on the operating system)

If you want to explicitly allow a thread to another thread to run an opportunity to one of the ways you can take the following
1, adjust the priority of each thread
2, so that in a running thread calls Thread.sleep () method
3, so that in a running thread calls Thread.yield () method
4, so that the thread is running another thread calls join () method

Adjust the priority of each thread
Thread class setPriority (int) and getPriority () methods were used to set priorities and reading priority.
If you want the program can shift the value to each operating system, should ensure that the priority of the thread in the set, only the use of MAX_PRIORITY, NORM_PRIORITY, MIN_PRIORITY these three priority.

Thread Sleep: When the thread is executed in the operation of sleep () method, it will abandon the CPU, to block the state.
Thread concession: When the thread is running in the implementation of the Thread class of the yield () static method, this time with the same priority, if the other threads in the ready state, then the yield () method will run the currently running thread into the pool make another thread to run. If you can not have the same priority thread is running, then the yield () method to do nothing.
Sleep () method and yield () method of Thread class are static methods, will cause the currently running thread to give up in the CPU, to run the chance to give other threads, the difference is:
1, sleep () method will give the opportunity to run other threads, regardless of the priority of other threads, it will give the opportunity to run a lower thread; yield () method will give the same priority or higher priority thread a chance to run.
2, when the thread executed sleep (long millis) method, will be transferred to blocking state, the parameter millis specify the sleep time; when the thread executed yield () method, will be transferred to ready state.
3, sleep () method declaration throws InterruptedException exception, but yield () method does not declare any exception thrown
4, sleep () method than the yield () method has better portability

Other threads to wait for the end: join ()
The currently running thread can call another thread join () method, the currently running thread will be transferred to blocking state until the end of another thread to run, it was restored to normal operation.

Timer Timer: The java.util package in the JDK provides a utility class Timer, it can from time to time to perform a specific task.

Thread synchronization atomic operation: According to the Java specification, for the basic types of assignment or a return value of operation is atomic operation. But here's the basic data types does not include long and double, because the JVM can see the basic storage unit is 32, while the long and double, should use 64 to represent. It can not be completed in one cycle.

Increment operations (+ +) is not atomic operation, as it relates to a read and a write.

Atomic operations: from a group of related operations completed, these operations may be manipulated with other threads to share resources, in order to ensure correct computing results of a thread in the implementation of atomic operations In the meantime, other measures should be taken to make the other threads can not be manipulation of shared resources.

Synchronized block of code: In order to ensure that each thread can be the normal execution of atomic operations, Java introduced a synchronization mechanism, the specific practice is on behalf of the atomic program code with synchronized pre-marked, so that the code is known as the synchronized block of code.

Sync Lock: Each JAVA object has and only a synchronization lock, at any time up to allow only one thread owns the lock.

When a thread attempts to access a synchronized (this) marked block of code, you must obtain this keyword references the object's lock, in the following two cases, this thread has a different fate.
1, if the lock has been occupied by other threads, JVM will put this thread into the lock of the object pool. This thread into the blocking state. Lock the pool may have a lot of threads, wait until the other thread to release the lock, JVM will be randomly removed from the lock a thread pool to make this thread has a lock, and go to ready state.
2, if the lock has not been occupied by other threads, this thread will get the lock to begin implementation of the synchronized block of code.
(Under normal circumstances in the implementation of synchronized block of code will not release synchronization lock, but there are special circumstances would be to release the object lock, such as in the implementation of synchronized block of code has encountered an exception which led to the termination of the thread, the lock will be released; in the implementation of the code block when the the implementation of the lock-owned object wait () method, the thread will release the object lock, enter the object's wait for the pool)

Thread synchronization features:
1, if a synchronized block of code and non-synchronized block of code simultaneously shared resources, will still result in a shared resource competition. Because when a thread executing a synchronized block of code object, other threads can still be the implementation of an object non-synchronous code block. (So-called synchronization between threads, to refer to different threads in the implementation of the same synchronized block of code of an object, because to obtain the object's synchronization lock to contain each other)
2, each object has a unique synchronization lock
3, in front of static methods you can use the synchronized modifier.
4, when a thread is started when the synchronized block of code does not mean that must run uninterrupted into the synchronized block of code thread can perform Thread.sleep () or perform Thread.yield () method, this time it does not release the object lock, just opportunities to run the other thread.
5, Synchronized statements will not be inherited, if a modification of the method with the synchronized class quilt cover, then the sub-class, this method does not keep pace, unless synchronized modifier.

Thread-safe class:
1, this class of object can simultaneously be multiple thread-safe access.
2, each thread can be a normal implementation of atomic operations, get the correct results.
3, in each thread of the atomic operations are completed, the object in a logical and reasonable state.

To release the object lock:
1, the implementation of synchronized block of code will end the release of the object lock
2, in the implementation of the synchronized block of code in the process of thread encountered an exception which led to the termination, the lock will be released
3, in the implementation of the synchronized block of code in the process of implementation of the lock object belongs wait () method, the thread will release the object lock, waiting to enter the object pool.

Deadlock when a thread is waiting for a lock held by another thread, while the latter has been the first one waiting thread holds a lock when the deadlock occurs. JVM does not monitor nor try to avoid this situation, thus ensuring deadlock does not occur became the programmer's responsibility.

How to avoid deadlock a common rule of thumb is: when several threads access a shared resource must be A, B, C, to ensure each thread with the same order to access them.

Thread communication
Java.lang.Object class provides two methods for thread communication
1, wait (): the implementation of this method is the thread releases the object lock, JVM will wait on an object in the thread pool. The thread is waiting for other threads wake up
2, notify (): the implementation of the method of the object to wait for the thread wake up in the pool waiting for a thread, JVM from a pool of randomly selected objects to wait for a thread, lock it to the object pool.

分类:Java 时间:2010-03-16 人气:267
分享到:
blog comments powered by Disqus

相关文章

  • java.lang.LinkageError: loader constraint violation: when resolving interface ... 2010-10-06

    java.lang.LinkageError: loader constraint violation: when resolving interface method "javax.servlet.jsp.JspApplicationContext.getExpressionFactory()Ljavax/el/ExpressionFactory;" the class loader (instance of org/apache/jasper/servlet/JasperLoade

  • java.lang.reflect.InvocationTargetException (in the configuration hibernate connection pool using proxool Times solution to this exception) 2010-07-21

    Exception: java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at

  • java.lang.OutOfMemoryError: Java heap spac 2010-09-25

    2010-0-9-25 The exception java.lang.OutOfMemoryError: Java heap spac Memory use: 1. Can not be over-reliance on GC 2. Monitoring the situation of memory --- learning of others maxMemory () This method returns the java virtual machine (the process) ca

  • Dalvik virtual machine 2010-03-29

    Google officially released at the end of 2007, a Android SDK, as the Android system, important features, Dalvik virtual machine for the first time into the people's vision. Its efficient use of memory, and in low-speed, high-performance CPU on the sh

  • S Android operating principle of virtual machine 2010-10-25

    Interpretation of the virtual machine works Android Android virtual machine under the following about the components and some structural principle, to introduce Android virtual machine, have to understand what is under the Android operating system, c

  • The basis of the virtual machine and android Dalvik 2010-11-14

    Dalvik virtual machine vendors such as Google Android mobile device platform developed by a core component of one. Dalvik is a third-party Java virtual machine is a virtual machine, it can support has been converted. Dex (ie Dalvik Executable) format

  • How do I change the size of the hard disk virtual machine vmware 2010-08-02

    Since the first use of the virtual machine, the first operation, the result points to the operating system hard disk when only points of the 8G, just not enough, then think of ways to drive expansion, as follows: 1. In the extended hard drive, make s

  • Bochs virtual machine under Linux use 2011-04-29

    1 virtual one floppy disk Component in the installed Bochs, has a tool called bximage, it can not only generate a virtual floppy disk, can also generate virtual hard disk, where we generate a virtual floppy disk (this is because the back of our test

  • JAVA virtual machine works (turn) 2011-09-01

    JAVA virtual machine works 2009-04-11 21:41:25 | Category: JAVA programming accumulation | Tags: | font size medium and small subscription Java Virtual Machine in between machines and compiler, on any platform are provided to the compiler a common in

  • java get CPU utilization, system memory, virtual machine memory and so on (without JNI) 2010-05-14

    Recently had an item is to get the system cpu share information, etc., first thought to use the dynamic link library, but later found to be like this do not call jni, this eliminates the need for many to see the new technology time o ( ∩ _ ∩ ) o. ..

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.684 (s). 10 q(s)