Copyright notice: this article is the blogger’s original article, without the permission of the blogger shall not be reproduced source: github.com/AnliaLee if you see any mistakes or good suggestions, welcome to leave a comment

preface

This is the first chapter of Android multithreading, so let’s start with thread creation. There are two common ways to create threads (we’ll talk about the Callable interface later when we introduce the Executor Framework).

  • Inherit the Thread class and override the run() method of Thread
  • Implement the Runnable interface, overriding Runnable’s run() method and instantiating it as a parameter to Thread

The topic of this chapter is to understand the connection and difference between Thread and Runnable by comparing how threads created in these two ways perform tasks

First the conclusion:

  • Thread implements the Runnable interface and overrides the run() method
  • Classes that implement Runnable are more robust and avoid the limitations of single inheritance. 2. Runnable is easier to share resources, allowing multiple threads to process a resource simultaneously

Comparison of Thread and Runnable

Let’s just tell a story using the classic ticketing system. There are two ticket scalpers, T (Thread) and Runnable (Runnable), each with two salesmen, usually doing the work of reselling tickets for events (Thread execution task). One day, they each got 5 tickets for a concert, and they gave the task of selling tickets to their salesmen

Here’s how the business works on the T side (subclass Thread and override the run() method, which is the ticketing task).

public class TicketThread extends Thread {
	private int ticket = 5;
	private String name;

	public TicketThread(String name) {
		this.name = name;
	}

	public void run(a) {
		for (int i = 0; i < 5; i++) {
			if (ticket > 0) {
				Log.e("T",name + "Sold a ticket, number T."+ (ticket--)); }}}}Copy the code

The salesman starts selling tickets early in the morning and quickly sells them (instantiate a subclass of Thread and call start() to start the Thread)

TicketThread t1 = new TicketThread("Salesman No. 1");
TicketThread t2 = new TicketThread("Salesman No. 2");

t1.start();
t2.start();
Copy the code

The business process for small R looks like this (implement the Runnable interface and override Runnable’s run() method)

public class TicketRunnable implements Runnable {
	private int ticket = 5;

	public void run(a) {
		for (int i = 0; i < 5; i++) {
			if (ticket > 0) {
				Log.e("R",Thread.currentThread().getName() + "Sold a ticket numbered R."+ (ticket--)); }}}}Copy the code

The salesman also sold out quickly (create Thread subclass instances, instantiate Thread objects that implement the Runnable interface as arguments, and call start() to start the Thread).

TicketRunnable runnable = new TicketRunnable();
Thread r1 = new Thread(runnable, "Salesman No. 1");
Thread r2 = new Thread(runnable, "Salesman No. 2");

r1.start();
r2.start();
Copy the code

After that, they decided to have a celebratory meal. After a exchange, R was surprised why T can sell the same ticket more than twice the money, small T proudly amli their own experience selling tickets:

“Because I made a copy of the ticket, so that my two younger brothers could sell a ticket to each other. (By inheriting Thread, when a new Thread is started, its bound task is also created, so that their tasks are independent of each other, so they cannot share resources.)

Little R sighs after hearing this:

“Is there such an operation? I honestly handed the ticket to both salespeople and let it go.”

Then small R asked small T: “you do so, afterwards not afraid to buy fake tickets to find you?” After listening to small T disapprovingly way:

“Afraid of nothing, we all run after one vote.”

R: “I have never seen such a brazen man……”

(To be continued…)


The next chapter forecast

His business is growing thanks to honesty, but he has recently received complaints from customers who say they bought fake tickets. Small R suspect is his hand stem, then the decision to recruit an individual to supervise his hand. One day, a man claiming to be synchronized comes to interview…