Java 多线程

2019-07-10  本文已影响0人  Roct

Thread

class MyThrad extends Thread { // 线程的主体类
    private String title;
    public MyThrad(String title) {
        this.title = title;
    }
    @Override
    public void run() { // 线程的主体方法
        for (int i = 0; i < 10; i++) {
            System.out.println("i:" + i + ",title:" + this.title);
        }
    }
}

public class JavaDemo {

    public static void main(String[] args) {
            new MyThrad("线程1").start();
            new MyThrad("线程2").start();
            new MyThrad("线程3").start();
    }
}
Thread执行分析

在java文件中调用start, java中使用JNI技术调用start0, jvm根据不同系统调用不同系统底层实现的函数, 实现资源分配

Runnable

class runnableImpl implements Runnable { // 线程的主体类
    private String title;
    public runnableImpl(String title) {
        this.title = title;
    }
    @Override
    public void run() { // 线程的主题方法
        for (int i = 0; i < 10; i++) {
            System.out.println("i:" + i + ",title:" + this.title);
        }
    }
}
public class JavaDemo {

    public static void main(String[] args) {
//        Thread
//        new MyThrad("线程1").start();
//        new MyThrad("线程2").start();
//        new MyThrad("线程3").start();



//        Runnable
        Thread threadA = new Thread(new runnableImpl("线程1"));
        Thread threadB = new Thread(new runnableImpl("线程2"));
        Thread threadC = new Thread(new runnableImpl("线程3"));
        threadA.start();
        threadB.start();
        threadC.start();

    }
}

Runnbale lambada

class runnableImpl implements Runnable { // 线程的主体类
    private String title;
    public runnableImpl(String title) {
        this.title = title;
    }
    @Override
    public void run() { // 线程的主题方法
        for (int i = 0; i < 10; i++) {
            System.out.println("i:" + i + ",title:" + this.title);
        }
    }
}

public class JavaDemo {

    public static void main(String[] args) {
//        Thread
//        new MyThrad("线程1").start();
//        new MyThrad("线程2").start();
//        new MyThrad("线程3").start();



//        Runnable
//        Thread threadA = new Thread(new runnableImpl("线程1"));
//        Thread threadB = new Thread(new runnableImpl("线程2"));
//        Thread threadC = new Thread(new runnableImpl("线程3"));
//        threadA.start();
//        threadB.start();
//        threadC.start();


//        Runnable lambada
        for (int i = 0; i < 3; i++) {
            String title = "线程对象" + i;
            Runnable run = () -> {
                for (int j = 0; j < 10; j++) {
                    System.out.println(title + "运行, j=" + j);
                }
            };
            new Thread(run).start();
        }

    }
}

Thread和Runnable的关系

打开Thread类的定义

public class Thread extends Object implements Runnable {}

发现现在Thread类也是Runnable接口的子类, 那么之前继承Thread类, 其实也是覆写的Runnbale

多线程开发本质: 多个线程可以进行同一个资源的抢占
多线程开发

Callable实现多线程

从最传统的开发来讲, 如果进行多线程的实现, 肯定依靠Runnable, 但是Runnable接口有一个问题, 线程执行完毕以后无法获取一个返回值, 所以在JDK1.5以后提出了一个新的线程实现接口, java.util.concurrent.Callable接口.

接口的定义
@FunctionalInterface
public interface Callable<V> {
        public V call() throws Exception;
}

可以发现Callable定义的时候可以设置一个泛型, 此泛型的类型就是返回值的类型, 这样的好处, 可以避免向下转型带来的安全隐患.

CallableThread的关系
`Callable`与`Thread`的关系
代码示例
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

class MyThread implements Callable<String> {
    @Override
    public String call() throws Exception {
     for (int i = 0; i < 10; i++) {
         System.out.println("**********线程执行, i=" + i);
     }
     return  "线程执行完毕";
    }

}
public class CallableDemo {
    public static void main(String[] args) throws Exception {
        FutureTask<String> task = new FutureTask<>(new MyThread());
        new Thread(task).start();
        System.out.println("线程返回数据," + task.get());
    }
}
output
**********线程执行, i=0
**********线程执行, i=1
**********线程执行, i=2
**********线程执行, i=3
**********线程执行, i=4
**********线程执行, i=5
**********线程执行, i=6
**********线程执行, i=7
**********线程执行, i=8
**********线程执行, i=9
线程返回数据,线程执行完毕
RunnableCallable的区别
只要想使用多线程, 必须使用Thread中的start()

多线程运行状态

对于多线程编写程序的流程: 定义线程主题类, 而后通过Thread类进行线程的启动, 但是并不意味着你调用了start()方法, 线程就已经开始运行了, 因为整体的线程处理有自己的一套运行的状态

线程运行状态
上一篇 下一篇

猜你喜欢

热点阅读