For some applications such as Server, you need to respond to concurrency, then you need to use multi-threading
Two ways to achieve multi-threading:
- Inherit Thread class
- Implement the Runnable interface
Inherit Thread class
Inherit the Thread class must override the run method , and then call the start
method, you can perform
Implement a thread class that prints numbers
package concurrent;
class printNumThread extends Thread
{
String thname;
printNumThread(String threadName) {
this.thname = threadName;
}
public void run() {
for(int i=0; i<=26; i++) {
System.out.printf("Thread name: %s , print num: %d\n", thname, i);
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class mutilThread {
public static void main(String[] args) {
printNumThread pt1 = new printNumThread("线程A");
pt1.start();
printNumThread pt2 = new printNumThread("线程B");
pt2.start();
}
}
Implement the Runnable interface
The Runnable interface can be rerun or not written, but it is worth noting that calling Runnable's run directly does not start multithreading. You need to pass in the object of the Runnable interface we implement in the Thread class constructor to start
Error demonstration: Implement a class that prints letters, you can see that the output is still serial
package concurrent;
class printCharThread implements Runnable {
String thname;
printCharThread(String threadName) {
this.thname = threadName;
}
public void run() {
for(char i='a'; i<='z'; i++) {
System.out.printf("Thread name: %s , print char: %c\n", thname, i);
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class mutilThread {
public static void main(String[] args) {
printCharThread pt3 = new printCharThread("线程C");
pt3.run();
printCharThread pt4 = new printCharThread("线程D");
pt4.run();
}
}
The correct method: call the class method if it is implemented
in the Thread
class constructorRunnable
Thread
start
package concurrent;
class printCharThread implements Runnable {
String thname;
printCharThread(String threadName) {
this.thname = threadName;
}
public void run() {
for(char i='a'; i<='z'; i++) {
System.out.printf("Thread name: %s , print char: %c\n", thname, i);
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class mutilThread {
public static void main(String[] args) {
printCharThread run3 = new printCharThread("线程C");
Thread pt3 = new Thread(run3);
pt3.start();
printCharThread run4 = new printCharThread("线程D");
Thread pt4 = new Thread(run4);
pt4.start();
}
}
Two methods: parallel
package concurrent;
class printNumThread extends Thread
{
String thname;
printNumThread(String threadName) {
this.thname = threadName;
}
public void run() {
for(int i=0; i<=26; i++) {
System.out.printf("Thread name: %s , print num: %d\n", thname, i);
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class printCharThread implements Runnable {
String thname;
printCharThread(String threadName) {
this.thname = threadName;
}
public void run() {
for(char i='a'; i<='z'; i++) {
System.out.printf("Thread name: %s , print char: %c\n", thname, i);
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class mutilThread {
public static void main(String[] args) {
printNumThread pt1 = new printNumThread("线程A");
pt1.start();
printNumThread pt2 = new printNumThread("线程B");
pt2.start();
printCharThread run3 = new printCharThread("线程C");
Thread pt3 = new Thread(run3);
pt3.start();
printCharThread run4 = new printCharThread("线程D");
Thread pt4 = new Thread(run4);
pt4.start();
}
}