Java并发-线程

线程和进程的区别

根本区别:进程是操作系统资源分配的基本单位,而线程是任务调度和执行的基本单位

开销:每个进程都有独立的代码和数据空间,程序之间的切换会有较大的开销;线程可以看做轻量级的进程,同一类线程共享代码和数据空间,每个线程都有自己独立的运行栈和程序计数器(PC),线程之间切换的开销小。

所处环境:在操作系统中能同时运行多个进程(程序);而在同一个进程(程序)中有多个线程同时执行(通过CPU调度,在每个时间片中只有一个线程执行)

内存分配方面:系统在运行的时候会为每个进程分配不同的内存空间;而对线程而言,除了CPU外,系统不会为线程分配内存(线程所使用的资源来自其所属进程的资源),线程组之间只能共享资源。

包含关系:没有线程的进程可以看做是单线程的,如果一个进程内有多个线程,则执行过程不是一条线的,而是多条线(线程)共同完成的;线程是进程的一部分,所以线程也被称为轻权进程或者轻量级进程。

Java 创建线程

继承Thread类

public class newThread extends Thread{
    @Override
    public void run() {
    }

    public static void main(String[] args) {
        new newThread().start();
    }
}

实现Runnable

package concurrent;

public class newThread{

    public static void main(String[] args) {
        final int i = 0;
        new Thread(new Runnable() {
            @Override
            public void run() {
                
            }
        }).start();
    }
}

实现Callable

package concurrent;

import java.util.concurrent.*;

public class NewThread implements Callable<Integer>{
    int cnt = 0;

    @Override
    public Integer call() throws Exception {
        for (int i = 0; i < 100; i++) {
            System.out.println(cnt++);
        }
        return new Integer(cnt);
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException{
        NewThread newThread = new NewThread();
        FutureTask<Integer> task = new FutureTask<>(newThread);
        new Thread(task).start();
        System.out.println(task.get());
    }
}

线程池后续讨论

线程状态及转换

在这里插入图片描述
图片摘自:Java并发编程的艺术

Thread有关的方法

start() 开始执行,注意该方法只能执行一次

sleep() 睡眠,不会释放锁

wait() Object类的方法,线程进入等待状态,会释放锁并且进入等待队列

notify() notifyAll()方法 Object类的方法,将等待状态的线程转换成就绪状态

yield()方法和sleep()方法类似,不会释放锁,但yield()方法不能控制具体交出CPU的时间,只能将cpu让给同优先级的线程

join()加入当前线程,等待调用join()方法线程结束后再执行,有参数的话是等待时间

以下是wait(), notify()用法示例。

package concurrent;

import java.util.concurrent.TimeUnit;

public class WaitAndNotifyUsage {
    private int  count;

    public static void main(String[] args) {

        WaitAndNotifyUsage usage = new WaitAndNotifyUsage();

        Thread notifyThread = new Thread(() ->{
                synchronized (usage){
                    for (int i = 0; i < 100; i++) {
                        usage.count = i;
                        if (i == 50) System.out.println(Thread.currentThread().getName() + " notify");
                    }
                }
        });

        Thread waitThread = new Thread(()-> {
                synchronized (usage){
                    System.out.println("开始等待");
                    try {
                        usage.wait();
                        System.out.println(Thread.currentThread().getName() + " 跳出等待");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("count = " + usage.count);
        });

        waitThread.start();
        try {
            TimeUnit.SECONDS.sleep(1);
        }catch (InterruptedException e) {
            e.printStackTrace();
        }
        notifyThread.start();
    }
}

interrupt, interrupted, isinterrupted()用法

package concurrent;

import java.sql.Time;
import java.util.concurrent.TimeUnit;

public class InterruptUsage {
    public static void main(String[] args) {
        Thread sleepThread = new Thread(() -> {
           try {
               TimeUnit.DAYS.sleep(1);
           }catch (InterruptedException e) {
               System.out.println("被打断");
           }

        });

        sleepThread.start();

        try {
            TimeUnit.SECONDS.sleep(1);
        }catch (InterruptedException e) {

        }
        Thread interruptThread = new Thread(() -> {
            sleepThread.interrupt();
        });
        interruptThread.start();

    }
}

捕捉异常,打断sleep。

package concurrent;

import java.sql.Time;
import java.util.concurrent.TimeUnit;

public class InterruptUsage {
    public static void main(String[] args) {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().isInterrupted());
                System.out.println(Thread.currentThread().isInterrupted());
                System.out.println(Thread.interrupted());
                System.out.println(Thread.interrupted());
            }
        });
        thread.start();
        thread.interrupt();
    }
}

true
true
true
false

interrupt()方法仅仅是在当前线程中打了一个停止的标记,并不是真正的停止线程,被打断后抛出异常

interrupted()测试当前线程是否已经是中断状态,执行后具有清除中断状态flag的功能

isInterrupted()测试线程Thread对象是否已经是中断状态,但不清除中断状态flag

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值