On the basis of the thread

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_44238154/article/details/102753521

Thread

About a thread in java based learning when it, but when the work is still the case with the newspaper more, it is more important, and here I simply collate it, and we want to help.

Five state of the thread

Thread (Thread) create ready to run blockage death

The basic operation of the thread

1.Thread.currentThread () getName ();. Get the current thread's name
2.Thread.currentThread () setName ( "This is my own definition of thread name");.
. 3.Thread.currentThread () setPriority (the Thread .MAX_PRIORITY); thread priority setting
4.Thread.currentThread () setPriority (7); . is provided between the thread priority 7 (1-10, default is. 5)
5.Thread.sleep (1000); // 4 milliseconds hibernation

Created on the thread

The first method is inherited thread rewrite run

Time of the call class names can not run in such a method is equal to or primary main thread running
thread has its own automated method {
myThread new new myThread M1 = ();
m1.start ();
that the thread becomes ready state, but a system to give will perform round-robin

}
Performed after starting the run method is the content of the thread

public class Mythread1_extends extends Thread{
	/**
	 * 线程的创建方式有两个 第一个就是通过 继承thread 
	 *  第二是通过实现implements Runnable接口
	 * 这个thread.currentThread().getName() 是来作用当前的进程的
	 */
	public void run() {
		for (int i = 0; i < 10; i++) {
			System.out.println("我是线程 "+Thread.currentThread().getName()+"执行的第"+i+"次");
		}
	}
	public static void main(String[] args) {
		//创建进程有两种方式  1.是继承 thread 的方式
		Mythread1_extends m1 =new Mythread1_extends();//创建进程
		m1.setName("线程A");//给进程起个名字
		m1.start();//就绪
		/*
		 *!!!!!! 这里如果直接 m1.run()  等于说还是调用的是main进程
		 */
		Mythread1_extends m2 =new Mythread1_extends();
		m2.setName("线程B");
		m2.start();
	}
}

The second interface is implemented runable run method

Way with inheritance way to achieve runadle bit different
need to thread into the thread of the class object to create an object, call the thread object start a thread (ready)

public class Mythread2_implement implements Runnable {
	
/**
 * 线程的创建方式有两个 第一个就是通过 继承thread  第二是通过实现implements Runnable接口
 */
	public void run() {
		for (int i = 0; i < 10; i++) {
			System.out.println("我是线程 "+Thread.currentThread().getName()+"执行的第"+i+"次");
		}
	}
	
	public static void main(String[] args) {
		//使用实现 implements runneble接口的方法
		  Mythread2_implement m2 =new Mythread2_implement(); //1首先要先创建进程
		  Thread t1 =new Thread(m2);//2调用thread的方法传入一个接口 这里我们使用的多态传入的是接口的一个实现类
		  t1.setName("接口实现的线程");//给这进程起一个名字
		  t1.start();//使其进入就绪状态
		  t1.setPriority(3);//设置优先级
		  System.out.println("优先级是:"+t1.getPriority());
		  System.out.println(t1.getId());//线程的标识符 Mythread2_implement m3 =new
		 
		  Mythread2_implement m3=new Mythread2_implement();//
		  Thread t2 =new Thread(m3);
		  System.out.println(t2.getId());//线程的标识符
	}
}

Knowledge is the basis of the above thread, I welcome you to whisper to discuss qq: 1500465781

Guess you like

Origin blog.csdn.net/weixin_44238154/article/details/102753521