Home  >  Article  >  Java  >  Implementation of multithreading in java

Implementation of multithreading in java

王林
王林forward
2019-11-28 17:00:292618browse

Implementation of multithreading in java

The first way is to define a subclass of the Thread class:

//第一种方法
public class MyThread extends Thread {
    @Override
    public void run() {
        String name = getName(); // 获取线程名称
        System.out.println(name); // 输出 Thread-0
    }
}
rrree

java Related video recommendations: java online learning

Enable multi-threading:

//第二种方法
public class MyThread extends Thread {
    @Override
    public void run() {
        Thread t = Thread.currentThread(); // 获取当前线程
        System.out.println(t); 	// 下面调用后输出 Thread[Thread-0,5,main]
        System.out.println(t.getName());// 输出 Thread-0
    }
}

The second way is to implement the Runnable interface:

Implementation steps:

1. Create an implementation class of the Runnable interface

2. Override the run method of the Runnable interface in the implementation class and set the thread task

3. Create an implementation class object of the Runnable interface

4. Create a Thread class object and pass the implementation class object of the Runnable interface in the constructor method

5. Call the start method in the Thread class to start a new thread to execute the run method

public class demo1Test {
    public static void main(String[] args) {
        MyThread mt = new MyThread();
        mt.start();// 会运行MyThread的run()方法
   }
}
public class Runnableimpl implements Runnable {//1.创建一个Runnable 接口的实现类
    @Override
    public void run() {
        for (int i = 1; i <= 20; i++) {// 2.在实现类中重写Runnable接口的run方法,设置线程任务
            System.out.println(Thread.currentThread().getName() + i);
        }
    }
}

The benefits of implementing the Runnable interface to create multi-threads:

1. Avoiding the limitations of single inheritance:

A class can only inherit one class, After a class inherits Thread, it cannot inherit other classes

It implements the Runnable interface, and can also inherit other classes and implement other interfaces.

2. Enhance the scalability of the program and reduce the coupling of the program

The way to implement the Runnable interface separates setting thread tasks and starting thread tasks

In the implementation class, override the run() method to set thread tasks.

Create a Thread class object and call the start() method to start a new thread

sleep() method to make the method sleep

public class demo2Test {
    public static void main(String[] args) {
        Runnable run = new Runnableimpl();//3.创建一个Runnable接口的实现类对象
        Thread t = new Thread(run); //4.创建Thread类对象,构造方法中传递Runnable接口的实现类对象
        t.start();//5.调用Thread类中的start方法,开启新的线程执行run方法
        for (int i = 1; i <= 20; i++) {
            System.out.println(Thread.currentThread().getName() + i);
        }	// Thread-0和main开始多线程抢夺cpu
    }
}

java Recommended related article tutorials: javaQuick Start

The above is the detailed content of Implementation of multithreading in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete