On thread Thread with Runnable

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_43548748/article/details/91127333

Beginner java, getting them interested in the thread mechanism.

There are two java multi-threading mechanism. Thread class and Runnable class.

  1. Learn Thread class to create a thread
  2. Learn Runnable interface to create threads
  3. The difference between the two

 

Thread class

 Thread class defined in the java.lang package, we use it only need to create a class that inherits it, while the lower classes run () method overrides (cover) it again.

The following are the easiest to create a thread using the Thread class method:

public class Text_Thread extends Thread{

	//重写run()方法
	public void run() {
		for(int i = 0;i <= 5;i++) {
			System.out.println("Szymou:" + i);
		}
	}
	
	public static void main(String[] args) {
		//建立线程对象
		Thread aThread = new Text_Thread();
		Thread bThread = new Text_Thread();
		
		//对象执行
		aThread.start();
		bThread.start();
	}

}

The above results are difficult to see in the thread switch execution, right? We rewrite:

public class Text_Thread extends Thread{

	private String a;
	//重新构造Text_Thread
	public Text_Thread(String a) {
		this.a = a;//this.a为此类的a
	}
	
	//重写run()方法
	public void run() {
		for(int i = 0;i <= 5;i++) {
			System.out.println("线程"+this.a+":" + i);
		}
	}
	
	
	public static void main(String[] args) {
		//建立线程对象
		Thread aThread = new Text_Thread("Szymou1");
		Thread bThread = new Text_Thread("Szymou2");
		
		//对象执行
		aThread.start();
		bThread.start();
	}

}

This can be clearly seen in the interactive execution of two threads.

 

Runnable Interface

We commonly used Runnable interface to create a thread.

The following is the method to use to create a thread Runnable interface:

public class Text_Runnable implements Runnable{

	private String a;
	//重新构造Text_Runnable
	public Text_Runnable(String a) {
		this.a = a;//this.a为此类的a
	}
	//重写run()
	public void run() {
		for(int i = 0;i <= 5;i++) {
			System.out.println("线程"+this.a+":" + i);
		}
	}
	
	public static void main(String[] args) {
		//创建Runnable接口对象
		Text_Runnable aThread = new Text_Runnable("Szymou1");		
		Text_Runnable bThread = new Text_Runnable("Szymou2");
		
		//由于Runnable定义下的子类中的没有start()方法,所以我们要依靠Thread的类来执行它。
		Thread aThread1 = new Thread(aThread);
		Thread bThread1 = new Thread(bThread);
		
		aThread1.start();
		bThread1.start();
	}

Since not Runnable class start () method, it is necessary to perform at start rely Thread. Because there is a constructor in the Thread class: public Thread (Thread target); This method may be performed instances of Runnable, the Thread class is started Runnable, i.e. can use it to start () to coordinate system resources to Runnable.

 

Thread and Runnable of difference

From the above results of the implementation point of view, it seems no different.

In fact, there is a difference, Thread class thread creation is not shared goal of resources, and Runnable interface to create objects that can be shared goal of resources. For example, absenteeism problem:

Note the following target object " kuangke " the use of variables

Thread:


public class Text_Thread extends Thread{

	private String a;
	private int kuangke = 5;
	//重新构造Text_Thread
	public Text_Thread(String a) {
		this.a = a;//this.a为此类的a
	}
	
	//重写run()方法
	public void run() {
		for(int i = 0;i <= 5;i++) {
			if(kuangke > 0) {
				System.out.println("宇某"+this.a+":" + (kuangke--) + "节");
			}
		}
	}
	
	
	public static void main(String[] args) {
		//建立线程对象
		Thread a = new Text_Thread("旷课");
		Thread b = new Text_Thread("旷课");
		Thread c = new Text_Thread("旷课");
		
		//线程对象执行
		a.start();
		b.start();	
		c.start();
		
	}

}

Runnable:

public class Text_Runnable implements Runnable{

	private String a;
	private int kuangke = 5;
	//重新构造Text_Runnable
	public Text_Runnable(String a) {
		this.a = a;//this.a为此类的a
	}
	//重写run()
	public void run() {
		for(int i = 0;i <= 5;i++) {
			if(kuangke > 0) {
				System.out.println("宇某"+this.a+":" + (kuangke--) + "节");
			}
		}
	}

	public static void main(String[] args) {
		//创建Runnable接口对象
		Text_Runnable aThread = new Text_Runnable("旷课");	
		
		//由于Runnable定义下的子类中的没有start()方法,所以我们要依靠Thread的类来执行它。
		Thread a = new Thread(aThread);
		Thread b = new Thread(aThread);
		Thread c = new Thread(aThread);
		a.start();
		b.start();
		c.start();
	}
	
}

由上面两个结果可知,Thread类的线程无法对其目标对象的资源共享,而是分开调用;Runnable创建的线程在使用同一目标对象时,目标对象的成员变量可以被这些线程共享。

 

Guess you like

Origin blog.csdn.net/weixin_43548748/article/details/91127333