The basic operation of the thread 1

Thread status: New,  thread just created, not executed

                     Runnable, ready, ready resources

                     Blocked, meet synchronized, enter obstruction suspended, waiting to acquire the lock.

                     Waiting, without time limit of waiting, usually waiting notify (), join () method is to wait for the target thread terminates.

                    TIme-Waiting,

                    Terminated; end

The basic operation of API java thread:

      1. Create a new thread

public class CreateThreads implements Runnable{
    
    @Test
    public void method1() {
        Thread t1  = new Thread() {
            @Override
            public void run() {
                System.out.println("I am fine!");
            }
        };
        t1.start();
    }
    

    @Override
    public void run() {
        System.out.println("I am runnable");
    }

    public static void main(String[] args) {
        Thread thread = new Thread(new CreateThreads());
        thread.start();
    }
}

 2. Stop

     stop () may be a thread exit, forced to terminate the thread easily lead to inconsistent data.

     For example: Data written to half, stop () end of the thread, release the current thread holds the lock, another thread holding the lock and read the data changes.

     Custom thread stops: a volatile variable, change the variable of the method, the logic stops.

3. interrupt thread

    It is important to thread interrupts thread coordination mechanism, 

InterruptThread class {public 
	/ ** 
	 * interrupt () interrupts the thread, only provides a prompt information to decide how to quit, quit immediately and unconditionally if equal to STOP () 
	 * isInterrupted () to determine whether the interrupt 
	 * interrupted () to determine whether the interruption, and clears the interrupt status 
	 * @throws InterruptedException 
	 * / 
	public static void main (String [] args) {throws InterruptedException 
		the Thread Thread new new = the Thread () { 
			@Override 
			public void RUN () { 
// custom exit logic while (Thread.currentThread () .isInterrupted ()) { System.out.println ( "interrupt Exit"); BREAK; } to Thread.yield (); } }; Thread.start (); Thread.interrupt (); } / ** * SLEEP () thread will sleep for some time and enter the blocked state, and will not release the lock, which means that if there are synchronized sync block other threads still can not access the shared data. Encounter interrupt will throw an exception * @throws InterruptedException * / @Test public void test2 () throws InterruptedException { the Thread the Thread = new new the Thread () { @Override public void RUN () { the while (to true) { IF (Thread.currentThread ( ) .isInterrupted ()) {
// the second cycle, the interrupt is checked out of the loop System.out.println ( "interrupted"); bREAK; } the try { the Thread.sleep (2000); } the catch (InterruptedException E) { System.out.println ( "interrupted in SLEEP"); // second interruption . Thread.currentThread () interrupt (); } Thread.yield (); } } }; Thread.start (); Thread.sleep (2000); // first break, then thread in the try section, triggering abnormal Add interrupted again Thread.interrupt (); } }

  

 

Guess you like

Origin www.cnblogs.com/zkfly/p/11494346.html
Recommended