The previous time made a use of multithreading to solve the query big data function, before is used. NET write, because of work needs, began to understand learning Java.
So, start to understand how multithreading is written in Java, how does thread synchronization proceed?
Before. NET inside writes the multi-threaded program, is also smattering, and does not use many threading characteristic, just very simple thread.start ();
Lock (obj)
{
}
To ensure that multiple threads of data are synchronized, and that even multiple threads decide whether to complete or not, or to use the main thread
while (counter!) =0) {
Thread.Sleep (100);
}
During this time, the thread synchronization problem was quite a headache for me, and the object collection was quoted in different ways, and the actions in different threads made me more frightened.
Finally, the big Data sub-table query function was successfully solved. (Think about it later, let the guys teach it).
Under. NET version of the code, now intends to convert to Java version of the code, the first intuitive image is that Java development extends inheritance.
Support for anonymous writing is not very good, the syntax is really not elegant, more bloated and cumbersome. (8 support for lambda expressions)
Back to multi-threading, first I need to know how to call, paste a piece of code (the code is found on the Internet):
/**
* Create a thread in a way that inherits the Java.lang.Thread class
*
* @author Dreamsea 2011-12-29 20:17:06
*/
public class ThreadTest extends Thread {
/**
* Override (Override) run () method The JVM automatically calls the method
*/
public void Run () {
System.out.println ("I ' m running!");
}
}
The second is to implement the interface.
/**
* Create a thread by implementing the Runnable interface
* @author Dreamsea
*/
public class ThreadTest2 implements Runnable {
public void Run () {
System.out.println ("I ' m running!");
}
}
It is important to note that the above ThreadTest2 class is simply an ordinary class that implements an interface.
If you use new ThreadTest2 (). run ();
Never run in a separate thread, not fooled by its thread name, it doesn't have a hair relationship with the thread, just a class that implements a common interface.
Unlike ThreadTest, which inherits the thread class, its base class already implements the relevant threading method.
New ThreadTest (). Start () is executed in a multi-threaded manner.
So the class that implements the interface, if it is executed as a multithreaded way? As follows:
ThreadTest2 tt = new ThreadTest2 ();
Thread t = new thread (TT);
T.start ();
Well, it knows how to use it.
So what's a multi-threaded thing? What is the difference from a single thread?
For example, we watch the movie, it is the computer constantly redraw the picture, let us see a real-time animation effect.
Moreover, we can watch the movie while we open chat software chat. It feels like the software is all in sync.
And here's what I understand.
Each program occupies one line, the instructions of different programs can be executed simultaneously.
The CPU can only execute instructions at a time, and can execute up to n instructions in the machine cycle.
The number of instructions that can be processed during a multi-core computer machine cycle is increased.
If it is not multithreaded, it will only run on one of the CPU's core commands.
In fact, multicore CPU utilization is not sufficient now, that is to say, the CPU instructions that can be processed in the machine cycle can actually increase a lot.
Single-threaded execution of 100 instructions, only one line of execution, assuming that each cycle can handle 5 instructions, 4 cores.
100/5=20 cycle time.
If we use multithreading to eliminate the competition of resources, we can process the 4*5=20 instruction in the cycle time.
100/20=5 cycle time.
This is the advantage of multi-core CPUs with multithreaded programs, as for single-core CPUs, multithreading does not help.
Here we find a thread state diagram, thanks (http://www.cnblogs.com/DreamSea/archive/2012/01/11/JavaThread.html)
As you can see, the start () of the thread does not necessarily mean that the multithreaded has been executed, after start (), the State is runnable (ready state),
The thread will then be run after being called by the JVM.
During the run, you can change the thread's running state by the blocked method through sleep (), join (), yiled (), or let the thread run state, change to the "Paused" state,
It then changes the paused state to a ready state by means of a related method, waiting for the JVM call to continue executing.
The Object.wait () method, and the Thread.Sleep () method are primarily known here.
What is the difference between changing the thread state to a paused state?
First, it is explicitly given that the sleep method is a timer to decide whether to return to the ready state for execution.
The Wairt method needs to receive a nofity () notification before it can continue execution.
To get a deeper understanding of these, it's important to understand the meaning of thread synchronization in order to understand why these methods are used.
It is possible for threads to share some resources with other threads, such as memory, files, databases, and so on.
Conflicts can arise when multiple threads read and write to the same shared resource at the same time. At this time, we need to introduce the thread "synchronization" mechanism, that is, there is a first served between the threads, you can not swarm squeeze up to grab a regiment.
The true meaning and literal meaning of thread synchronization is exactly the opposite. The true meaning of thread synchronization is actually "queued": there are several threads to queue, one to operate on shared resources, and not to operate concurrently.
Methods for thread synchronization
(1) Wait (): causes a thread to be in a wait state and releases the lock of the object held.
(2) Sleep (): Makes a running thread sleep, is a static method that calls this method to capture
Interruptedexception exception.
(3) Notify (): Wake up a waiting thread, note that when this method is called, it is not exact
A thread that wakes up a waiting state, but is determined by the JVM to wake up which thread, and not by priority.
(4) Notityall (): Wakes all the threads into the waiting state, noting that it is not a lock for all wake-up threads an object,
But to make them compete.
Object.wait () method, and Thread.Sleep ()
Note that these methods are synchronized for the thread.
- private void Mainloop () {
- while (true) {
- ....
- Synchronized (queue) {
- .....
- if (!taskfired)//Task hasn ' t yet fired; Wait
- Queue.wait (Executiontime-currenttime);
- }
- }
As above, if the wait () method is called, it actually means that the current thread no longer prevents other threads from continuing, because in the synchronized code block, the method inside the code block must all be executed before the next thread can execute.
Wait just to say, well, I also need certain conditions, you continue to carry out, you have done good to inform me. The main is to give up execution of the right to exit the block. (Wait is the basic method of an Object object, that is, an object that is a lock on objects, and object locks are explained later.) )
The Sleep () method will always wait there, blocking other threads from executing.
Well know this difference is very important, and then look back to explain the next synchronized in the object lock.
1, for the synchronous method or code block, must obtain the object lock to be able to enter the synchronous method or the code block to operate;
2, if the method-level synchronization, the object lock is the object of the method, if it is static, object lock refers to the method where the
Class object (unique);
3, for the code block, the object lock refers to the ABC in synchronized (ABC);
So let's look at these lock objects:
String Lock=new string ("");
threada{
Private String lock;
Public (String Lock) {lock=lock;}
Run () {
Synchronized (lock) {
}
}
}
Main () {
String Lockobj=new string ("lock");
New Threada (Lockobj). Start ();
New Threada (Lockobj). Start ();
New Threada (Lockobj). Start ();
}
In Java, Lockobj is a value reference, and this is the object in the main thread, so each thread actually locks the object in the same way and is the Lockobj object inside the main method.
Static synchronized run () {
}
Here synchronized refers to the object of the method itself, because the static is set, so this is not for the object of the class, but the class itself.
Well, after work, the basic concept has been clarified.
Java Multithreading Learning and summarization (I.)