Thread basic operations:
1, create a thread: only need a new thread object to: Thread myThread = new Thread (). Then myThread.start () to open threads. So the question is, this thread in the end what to do about it, so, Thread has a run () method, the method body is a thread to do. And by default, run () method what not to do, so we need to override the run (), you have to do to fill in.
public static void main(String[] args) { Thread t1=new Thread(){ @Override public void run() { System.out.println("hello i am t1"); } }; t1.start(); }
The above use anonymous inner classes rewrite a run method.
Because Java is a single inheritance, succession is also sometimes a scarce resource, so you can use Runable interface to achieve:
public interface Runnable{ public abstract void run(); }
Runnable interface only one run method.
Thread class there is an important constructor: public Thread (Runnable target), therefore, can be created by implementing Runnable interface class strength thread, at the same time, it is a function Runnable interface, so you can also use Lambda expressions:
public static void main(String[] args) { Thread t1=new Thread(()-> System.out.println("hello i am t1")); t1.start(); }
2, terminate the thread: Thread.stop () to terminate a thread, but it is a waste of method, because it is too violent, even if the thread is being implemented, will direct the termination and release the lock.
3, thread interrupts: interrupt thread does not make the thread exits immediately, but will send a notification to the thread, tell the target thread: Some people want you to quit it! As for how to deal with the target thread or threads and target their own decisions.
Three methods of interrupting the thread: Thread.interrupt () interrupt thread. Whether Thread.isInterruputed () judgment was discontinued. Whether Thread.interrupted () interpretation is interrupted, and clears the interrupt status.
public static void main(String[] args) throws InterruptedException { Thread t=new Thread(){ @Override public void run() { while (true) System.out.println("t1 is running."); } }; t.start(); Thread.sleep(1000); t.interrupt(); }
The above code, thread t has been interrupted, but did not make the operation after the break, so there is no effect. Improve:
public static void main(String[] args) throws InterruptedException { Thread t=new Thread(){ @Override public void run() { while (true){ if (Thread.currentThread().isInterrupted()) break; System.out.println("t1 is running."); } } }; t.start(); Thread.sleep(1000); t.interrupt(); }
4, wait (wait) and notification (notify): To support collaboration between multiple threads, JDK provides a wait () and notify (), these two methods are not in the Thread class, but in the Object, that is that any object can call these two methods.
When the thread A calls obj.wait () method, thread A will stop execution into a wait state. Waiting for the state to wait until another thread invokes obj.notify () method so far.
When a thread calls object.wait after () method, the thread will go in the waiting queue object object. The wait queue, there may be multiple threads. When you call object.notify (), randomly wake up a thread in the queue. And when you call object.notifyAll (), it wakes up all the threads.
Note: wait () and notify () must be in the synchronized statement, you must first obtain the listeners that the target object to calling these two methods. After calling wait, it will release the listener.
5, suspend (suspend) and continue (resume): suspend thread execution can be stopped until the resume. But suspend stop, will not release the resource, it is not recommended.
6, wait for the end of the thread (join) and humility (yeild): When the collaboration between the threads, sometimes have relations, that is, until the thread B A thread of execution is over, and then executed.
volatile:
When a variable declared with the volatile keyword, it tells the virtual machine, this variable is likely to be some of the threads modifies the virtual machine will do something to ensure that all the threads of this variable back after modification, within the scope of application this change can be seen. With variable volatile stated assures an orderly and visibility, but can not guarantee atomicity.
Daemon thread:
Daemon thread is a special thread, which is the guardian of the system, the completion of some systematic service in the background, such as garbage collection thread. The corresponding user threads. When a Java application only daemon threads, Java virtual machine will naturally exit. setDaemon () can be set to a thread daemon thread.
public class test { public static class DaemonThread extends Thread{ @Override public void run() { while (true){ System.out.println("i am alive"); } } } public static void main(String[] args) throws InterruptedException { Thread thread=new DaemonThread(); thread.setDaemon(true); thread.start(); Thread.sleep(2000); } }
Set the thread as a daemon thread, so after the main thread to sleep 2s, users all over the thread, the whole program will be over.
Thread priority:
High priority thread when competing for resources, will have an advantage. But this is only the probability of problems, bad luck, high-priority thread may fail to seize resources. setPriority () to set the thread priority.
public class test { final static Object obj=new Object(); public static class Low extends Thread{ static int i=0; @Override public void run() { while (true){ synchronized (obj){ i++; if (i>100000){ System.out.println("low complete。"); break; } } } } } public static class High extends Thread{ static int i; @Override public void run() { while (true){ synchronized (obj){ i++; if (i>150000){ System.out.println("high complete."); break; } } } } } public static void main(String[] args) { Thread low=new Low(); Thread high=new High(); low.setPriority(Thread.MIN_PRIORITY); high.setPriority(Thread.MAX_PRIORITY); low.start(); high.start(); } }
The above code, even if you want to pay more high 50,000, but because of the high priority, would be more first executed.
Synchronized:
Keyword synchronized action is to achieve synchronization between threads. Its job is to lock the synchronization code, so that each time only one thread to enter the fast synchronization, thus ensuring security between threads.
Usage: to the specified object locking can be (in the above examples), can also be applied to the example method:
public class test implements Runnable { static test t=new test(); static int i=0; public synchronized void add(){ i++; } @Override public void run() { for (int k=0;k<100000;k++) add(); } public static void main(String[] args) throws InterruptedException { Thread t1=new Thread(t); Thread t2=new Thread(t); t1.start(); t2.start(); t1.join();t2.join(); System.out.println(i); } }