_Common methods of 124 java thread

Posted by ricerocket on Wed, 25 Dec 2019 21:09:02 +0100

/*Common methods of Thread:
* 1. start(): start the thread and execute the corresponding run method.
* 2. run(): the code to be executed by the child thread is put into the run method.
* 3. currentThread(): static, calling the current thread.
* 4. getName(): get the name of this thread.
* 5. setName(): set the name of this thread.
* 6. yield(): the thread calling this method releases the execution right of the current CPU.
* 7, join(): the method of calling B thread in A thread indicates that when A is executed, the A thread stops executing until the B thread is executed, and then the A thread continues to execute the code after join().
* 8. isAlive(): judge whether the current thread is still alive.
* 9. sleep(long time): the displayed sleep time milliseconds for the current thread.
* 10. Thread communication: wait(); notify(); notifyAll();
 * 
* 11. Set thread priority
* (1)getPriority(): returns the priority of the current thread.
* (2)setPriority: sets the priority of the current thread.
 * 
 * */

import org.junit.Test;

/*Thread Common methods:
 * 1,start():Start the thread and execute the corresponding run method.
 * 2,run(): The code to be executed by the child thread is put into the run method.
 * 3,currentThread():Static, calls the current thread.
 * 4,getName(): Gets the name of this thread.
 * 5,setName():Set the name of this thread.
 * 6,yield():The thread calling this method releases the execution rights of the current CPU.
 * 7,join():The method of calling B thread in the A thread indicates that when the method is executed, the A thread stops executing until the B thread is executed, and the A thread continues to execute the code after join().
 * 8,isAlive():Determine whether the current thread is still alive.
 * 9,sleep(long time): time milliseconds for the current thread to sleep.
 * 10,Thread communication: wait(); notify(); notifyAll();
 * 
 * 11,Set thread priority
 * (1)getPriority():Returns the priority of the current thread.
 * (2)setPriority:Sets the priority of the current thread.
 * 
 * */


public class _001_TheMethodOfThread {
	
	@Test
	public void test() {
		//Create a sub object
		_001_TheMethodOfThread.SubThread subThread = new _001_TheMethodOfThread.SubThread();
		//Set thread name
		subThread.setName("Sub thread 1");
		//Startup thread
		subThread.start();
		
		for(int i=0;i<=100;i++) {
			System.out.println(Thread.currentThread().getName()+"   "+i);
			if(i%10==0) {
				Thread.currentThread().yield();
			}
		}
	}
	
	@Test
	public void testA() {
		//Create a sub object
		_001_TheMethodOfThread.SubThread subThread = new _001_TheMethodOfThread.SubThread();
		//Set thread name
		subThread.setName("Sub thread 1");
		//Startup thread
		subThread.start();
		
		for(int i=0;i<=100;i++) {
			System.out.println(Thread.currentThread().getName()+"   "+i);
			if(i-10==0) {
				try {
					subThread.join();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	@Test
	public void testB() {
		//Create a sub object
		_001_TheMethodOfThread.SubThreadA subThread = new _001_TheMethodOfThread.SubThreadA();
		//Set thread name
		subThread.setName("Sub thread 1");
		subThread.setPriority(Thread.MAX_PRIORITY);
		//Startup thread
		subThread.start();
		
		for(int i=0;i<=100;i++) {
			System.out.println(Thread.currentThread().getName()+"   "+Thread.currentThread().getPriority()+"   "+i);
		}
	}

	
	//InnerClass
	private static  class SubThread extends Thread{
		@Override
		public void run() {
			// TODO Auto-generated method stub
			for(int i=0;i<=100;i++) {
				try {
					Thread.currentThread().sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println(Thread.currentThread().getName()+"   "+i);
			}
		}
	}
	
	//InnerClass
	private static  class SubThreadA extends Thread{
		@Override
		public void run() {
			// TODO Auto-generated method stub
			for(int i=0;i<=100;i++) {
				
				System.out.println(Thread.currentThread().getName()+"   "+Thread.currentThread().getPriority()+"   "+i);
			}
		}
	}
	
}

 

Topics: Junit