Multithreaded note 3 (Java)

Keywords: jvm Attribute network

#Class notes, only you can read them

First, some states of threads:
Thread control joint control
Third, background thread

Note 1:

/*
* Deadlock:
*It can't be solved, it can only be avoided
*jvm does not detect and avoid, so program source should avoid deadlock.
*Thread class:
*suspend()
*Both methods of resume() have been deprecated, resulting in deadlock
*
*How to avoid deadlock:
*Ensure that each thread accesses in a certain order
*
*Thread blocking: someone synthesizes blocking state, waiting state and timing state into blocking state
*
*Thread life cycle: the process of thread from birth to death
*
*/

Note 2: (analyze the state of thread life cycle)

import javax.print.attribute.standard.MediaSize.Other;



class other extends Thread{
	public void run() {
		
		super.run();
		System.out.println("start !");
		
		
	}
	
	
	
}
public class RunnableDemo {
	
	
	

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		System.out.println("begiin");
		
		other other=new other();
		other.start();
		
//		other.start(); can only be started once, otherwise an error will be reported.
		
		
		/*
		 * 
		 * New new
		 * Use new to create thread objects, and only allocate memory space in the heap.
		 * Before calling the start method again, in the new state, the thread does not start.
		 * Just a thread object
		 * 
		 * 
		 * Operational status
		 *1)ready In ready state, the thread object calls start and waits for the jvm to schedule. (at this time, the thread is not running.)
		 *
		 *2)running Running state: the thread object gets the jvm schedule. If there are multiple CPUs, multiple threads are allowed to run in parallel.
		 *
		 *Blocking status:
		 *The running thread encountered cpu abandonment due to magic seed
		 *Pause operation, it will enter blocking state
		 *	 *Waiting status:
		 *Can only be woken by other threads, using the wait method without parameters
		 *1)The wait method is called when the county is in the running process
		 *At this point, the jvm puts the thread in the object waiting pool.
		 *2)Current thread enters sleep state
		 *
		 *Timing wait status:
		 *Use the wait method with parameters at this time
		 *1)Call wait (long time)
		 *2)Call sleep (long time)
		 *
		 *Termination status:
		 *That is, thread termination
		 *1)Exit after executing the run method
		 *2)Unexpected exit (unexpected termination)
		 *
		 */
		
		

		

	}

}

Note 3: (thread sleep)

public class SleepDemo {
/*
 * 
 * Thread sleep:
 * 
 * Pause the executing thread for a while and enter the timing waiting time
 * sleep(long millis)
 * After calling sleep, the current thread abandons the cpu
 * This method is used to simulate network delay.
 * The error effect of having multiple threads concurrent with the same resource is more obvious
 * 
 * 
 * 
 */

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		for(int i=10;i>0;i--) {
			
			System.out.println("Left"+i+"second");
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
			
		}
		
		System.out.println("boom!");
		
		

	}

}

Note 4: (thread control joint control)

class join extends Thread{
	
	@Override
	public void run() {
		// TODO Auto-generated method stub
		super.run();
		for(int i=0;i<50;i++) {
			System.out.println("join"+i);
			
		}
	}
}



public class JoinDemo {

/*
 * The join method of a thread, which means that one thread waits for another thread to complete before executing. After the join method is called, the thread object appears blocked.
 * Called Union thread, it is the current thread and the current thread.
 * 
 * 	
 */

	public static void main(String[] args) throws InterruptedException {
		// TODO Auto-generated method stub
		
		System.out.println("begin..");
		
		join joinThread=new join();
		
		for(int i=0;i<50;i++) {
			
			System.out.println("main"+i);
			if(i==10) {
				joinThread.start();
				joinThread.join();
				//Indicates that one thread waits for another thread to complete before executing.
			}
//			if(i==20) {
//				joinThread.start();
//			}
			
		}
		
		System.out.println("end....");
		
		

	}

}

Note 5: (background thread)

/*
 * Run in the background to serve other threads, also known as "daemons"“
 * List as: GC of jvm
 * 
 * Characteristic:
 * All foreground threads end, background threads end
 * 
 * How to test whether it is a background thread:
 *Thread.isDaemon()
 *
 *The default thread created by foreground thread is foreground thread.
 *It can be set as background thread through setDaemon() method
 * 
 * Set to background thread must be called before start.
 * 
 * Self-taught:
 * thread priority 
 * Thread comity
 * Timers and thread groups
 * 
 * 
 */


//

class DaemonThread extends Thread{
	
	@Override
	public void run() {
		// TODO Auto-generated method stub
		super.run();
		
		for(int i=0;i<50;i++) {
			
			System.out.println(super.getName()+" "+super.isDaemon()+i);
		
		}
	}
}
public class DomeThread {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
//Cannot set main thread as background thread
		//Thread.currentThread().setDaemon(true);
		
		//Test whether it is a background thread
		System.out.println(Thread.currentThread().isDaemon());
		
		for(int i=0;i<50;i++) {
			
			System.out.println("main"+i);
			if(i==10) {
				DaemonThread thread=new DaemonThread();
				thread.setDaemon(true);
				thread.start();
				
			}
		}
	}

}

Posted by homerjsimpson on Fri, 25 Oct 2019 14:36:28 -0700