Http://www.ibm.com/developerworks/cn/java/j-thread/index.html
Http://www.ibm.com/developerworks/cn/java/multithreading/index.html
Http://www.ibm.com/developerworks/cn/education/java/j-threads/j-threads.html
The concept of threading is basically clear, the key is to access shared variables, be sure to pay attention to.
Volatile: For simple variables, it is sufficient to use the volatile keyword, which represents a synchronization variable, and when volatile is accessed in multiple threads, it is necessary to ensure that all accesses to the variable are synchronized. This is useful, for example, if you have 100 threads open and you are running, and you want to close those threads with a tag variable, then this variable can be implemented.
Thread creation, either extends thread, or implement Runnable interface
1. Creating Threads by inheriting thread
2. Creating threads from the Runnable interface
This interface has only one function, run (), and this function must be implemented by the class that implements the interface. However, in the case of running this class, the semantics are slightly different from the previous one, and we can overwrite the previous example with the Runnable interface. (different parts are represented in bold.) )
Note that when using the Runnable interface, you cannot directly create an object of the desired class and run it; You must run it from within an instance of the thread class. Many programmers prefer the runnable interface because inheriting from the thread class imposes a class hierarchy.
Start of Thread, start ()
The end of the thread, run () is finished executing, or throws an exception, or another thread calls a deprecated stop () method. (TheStop method has not been advocated for use )
The join thread, join () method, when invoked Thread.join() , will block the calling thread until the target thread finishes.
Hibernate, the current thread enters the wait state, the sleep () method, until after a period of time, or another thread calls the interrupt () method on the thread object of the current threads, thus terminating the waiting thread, if the thread is by the Thread.Interrupt () Called by the terminal, then the dormant thread throws interruptedexception so that the thread knows it is awakened by an interrupt and does not have to see if the timer expires.
Thread.yield () Like sleep, it does not cause hibernation, but only pauses the current thread for a moment so that other threads can run, and in most implementations, Thread.yield() lower-priority threads do not run when higher-priority threads are called.
同步: SynchronizedThere are two important meanings: it ensures that only one thread can execute the protected part of the code at a time, and it ensures that the data changed by one thread is visible to other threads.
Lock: Each Java object has a related lock. Only one thread can hold a Java lock at a time. When a thread enters a synchronized block of code, the thread blocks and waits until the lock is available, obtains the lock when it is available, and executes the code block. When control exits a protected block of code, it reaches the end of the code block or throws asynchronized块中捕获的异常时,它就会释放该锁。
The synchronized simplest way to create a block is to declare the method as synchronized . This means that the caller must obtain a lock before entering the method body:
When do I need to sync?
In some cases, you do not have to use synchronization to pass data from one thread to another because the JVM has implicitly performed synchronization for you. These situations include:
- When the data is initialized by a static initializer (either on a static field or in
static{} a block initializer)
- When you access the final field
- When an object is created before the thread is created
- When a thread can see the object it will be working on
Java Multithreading (GO)