Java基础知识总结(69)

    (1)线程与进程

   

        线程:一个在内存中运行的应用程序。每个进程都有自己独立的一块空间,一个进程可以有多个线程。

        线程:是操作系统能够进行运算调度的最小单位。它被包含在进程之中,是进程中的实际运作单位。一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线程并行执行不同的任务。

        所有运行中的任务通常对应一个进程(Process)。当一个程序进入内存运行时,即变成一个进程,堆栈、自己的程序计数器和自己的局部变量,但不拥有系统资源,它与父进程的其他线程共享该进程所拥有的全部资源。

       

    (2)并行并发

   

        串行:排队依次执行。如一台打印机资源,10位同学排队打印文件,排队使用

        并行:同一时刻多条指令在多个处理机上运行。如两台打印机资源,10位同学排队打印文件,此时可以两位同学同时打印

        并发:同一时刻只能有一条指令执行,但多个进程指令被快速轮换执行,使得在宏观上具有多个进程同时执行的效果。

   

    (3)线程的创建

           

        1、继承Thread

   

        Thread

public class Thread implements Runnable {

    //常用构造方法

    public Thread() {

        this(null, null, "Thread-" + nextThreadNum(), 0);

    }

    public Thread(Runnable target) {

        this(null, target, "Thread-" + nextThreadNum(), 0);

    }

    public Thread(Runnable target, String name) {

        this(null, target, name, 0);

    }

    //常用API

    /**

     * 获取线程的状态,返回值是State枚举(见下文)

     */

    public State getState() {

        // get current thread state

        return jdk.internal.misc.VM.toThreadState(threadStatus);

    }

}

     

/**

 * 继承Thread类

   */

   public class MyThread extends Thread{

   @Override

   public void run() {

       System.out.println(Thread.currentThread().getName());

       System.out.println("MyThread Running");

   }

   public static void main(String[] args) {

       MyThread myThread = new MyThread();

       myThread.setName("MyThread");

       myThread.start();

   }

   }

因为Java中类是单继承的,继承了Thread类,就无法继承其他父类,不利于代码扩展

        2.继承Runnable接口

   

        Runnable

public class MyThread2 implements Runnable{

    public static void main(String[] args) {

        MyThread2 myThread2 = new MyThread2();

        Thread thread = new Thread(myThread2);

        thread.start();

    }

    @Override

    public void run() {

        System.out.println("线程执行体");

    }

}

        弊端:

   

        因为Runnable接口中没有start()方法,因此启动线程时,必须将继承了Runnable接口的类的对象通过Thread类的构造进行包装,然后调用Thread#start()方法启动线程。

        run()方法不能抛出异常,如果run()方法中代码抛出异常,程序只能中断,不能通过try...catch进行异常。

        run()方法没有返回值

   

        3.Callable接口

   

        Callable接口

@FunctionalInterface

public interface Callable<V> {

    /**

     * 等价于run()方法,但是该方法有返回值,并且可以抛出异常

     *

     * @return computed result

     * @throws Exception if unable to compute a result

     */

    V call() throws Exception;

}

        Future接口

public interface Future<V> {

    /**

     * 尝试取消一个任务.如果任务已经执行完成,则取消失败 . 当任务没有开始时,可以取消

     * 如果任务已经开始,线程能不能被取消取决于mayInterruptIfRunning参数

     *

     * 当调用完此方法后调用isCancelled(),isDone()方法都会返回true

     * @param mayInterruptIfRunning 如果是true,则正则执行的线程可以被取消,否则

     * 线程被允许执行完成。

     * @return {@code false} 如果返回false则线程没有取消

     * 典型的例如,这个线程已经执行完成。

     * {@code true} otherwise

     */

    boolean cancel(boolean mayInterruptIfRunning);

   

    /**

     * Returns {@code true} 如果任务在任务启动前或者完成过程被取消,则返回true

     * 在完成后取消,并调用该方法时返回false。

     *

     */

    boolean isCancelled();

   

    /**

     * Returns {@code true} 如果任务完成则返回true.

     * 任务被中断,被取消,或者发生异常,都会返回true

     *

     * @return {@code true} if this task completed

     */

    boolean isDone();

   

    /**

     * 如果需要,等待call()方法执行结束后,获取结果

     *

     * @return the computed result

     * @throws CancellationException if the computation was cancelled

     * @throws ExecutionException if the computation threw an

     * exception

     * @throws InterruptedException if the current thread was interrupted

     * while waiting

     */

    V get() throws InterruptedException, ExecutionException;

   

    /**

     * 等待给定时间内获取结果,如果在等待时间内没有获取到结果,则抛出异常

     *

     * @param timeout the maximum time to wait

     * @param unit the time unit of the timeout argument

     * @return the computed result

     * @throws CancellationException if the computation was cancelled

     * @throws ExecutionException if the computation threw an

     * exception

     * @throws InterruptedException if the current thread was interrupted

     * while waiting

     * @throws TimeoutException if the wait timed out

     */

    V get(long timeout, TimeUnit unit)

        throws InterruptedException, ExecutionException, TimeoutException;

}

  • 7
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

好教员好

您的鼓励是我前进动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值