Java学习日记(7月29)

今天学习了关于创建线程的三种方法:Callable,Thread,Runnable。

1.实现Callable接口,这种方法好在有返回值,但是编程过程相对复杂。

        与其他两种写法不同的是,他重写的是call()方法;同时用Callable接口的类 要在<>里指定返回类型,并且关于该类对象的操作要经过FutureTask<>,比如:

        Callable callable = new July29thCallable(startSum01);
        FutureTask f1 = new FutureTask<>(callable);
        Thread t1 = new Thread(f1);
        t1.start();
        try {
            String result1 = f1.get();
            System.out.println("The sum " + startSum01 + result1);

        /**注意:result是call()方法返回的结果,用FutureTake.get()方法获取*/


        } catch (Exception e) {
            e.printStackTrace();
        }

2.继承Thread类,这种方法好在编程简单,但是不能用于已经继承其他类的类。

        可以直接用一个类继承Thread类,然后在里面重写run方法就行,甚至可以在构造函数里面用super(name)设置线程名。

        之后在主类里面用 MyThread p = new Mythread("NO.1");
                                       Thread t = new Thread(p);
                                       t.start();

        或者类似: (new MyThread("No.1")).start();

                               //这里的MyThread继承了Thread类,所以可以直接start()

3.实现Runnable接口,这种方法我觉得就是Thread类的一个补充,用于那些已经继承其他类的类。

        跟Thread的方法是一样的,只是最后执行的时候要(new Thread(new MyRunnable()).start

下面代码是我对于三种方法的一个总结:

package JavaOrigin;

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

public class July29thCreateThread {
    public static void main(String[] args) {
        runJuly29thCallable(3);
        runJuly29thCallable(6);
        new July29thThread("Sum9", 9).start();
        new July29thThread("Sum12", 12).start();
        new Thread(new July29thRunnable("Sum15", 15)).start();
        new Thread(new July29thRunnable("Sum18", 18)).start();
    }

    private static void runJuly29thCallable(int startSum01) {
        Callable callable = new July29thCallable(startSum01);
        FutureTask f1 = new FutureTask<>(callable);
        Thread t1 = new Thread(f1);
        t1.start();
        try {
            String result1 = f1.get();
            System.out.println("The sum " + startSum01 + result1);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

class July29thCallable implements Callable {
    private final int n;

    July29thCallable(int n) {
        this.n = n;
    }

    @Override
    public String call() {
        int sum = 0;
        for (int i = 1; i < n + 1; i++) {
            sum += i;
        }
        return " The result of July29thCallable is " + sum;
    }
}

class July29thThread extends Thread {
    private final int n;

    July29thThread(String name, int n) {
        super(name);
        this.n = n;
    }


    @Override
    public void run() {
        int sum02 = 0;
        for (int i = 1; i < n + 1; i++) {
            sum02 += i;
        }
        System.out.println(Thread.currentThread().getName() + " The result of July29thThread is " + sum02);
    }
}

class July29thRunnable implements Runnable {
    private final int n;
    private final String name;

    public July29thRunnable(String name, int n) {
        this.name = name;
        this.n = n;
    }

    @Override
    public void run() {
        int sum03 = 0;
        for (int i = 1; i < n + 1; i++) {
            sum03 += i;
        }
        System.out.println(name + " The result of July29thRunnable is" + sum03);
    }
}

你可能感兴趣的:(java,学习,jvm)