yamlln

导航

并发、多线程

1、并发编程三要素

(1)原子性:一个或者多个操作,在执行过程中不能被打断;或者全部不执行; (2)可见性:多线程共享变量时,变量修改后,其他线程可以立即看到 (3)有序性:执行顺序为代码中的顺序

2、实现可见性的方法有哪些?

lock || synchronized:同一时刻只有一个线程能够获取锁执行代码,在锁释放之前将至存入内存

3、多线程价值

(1)发挥多核CPU优势:可以同时做几件事互不干扰 (2)防止阻塞:单核cpu时,如果一个线程阻塞,则程序停止运行;多线程则会改变此情况 (3)便与建模

4、创建线程的方式

(1)继承Thread类创建线程类
new Thread(()->{
System.out.printIn("ttttt");
}).start();
(2)通过 Runnable 接口创建线程类
public class Test{
    public static void main(String[] args){
        T1 t1 = new T1();
        Thread thread = new Thread(t1);
        thread.start();
            T2 t2 = new T2();
    Thread thread1 = new Thread(t2);
    thread1.start();
    }
}
​
class T1 implements Runnable{
    int count = 0;
    @Override
    public void run() {
        while (true){
            System.out.println("线程" + (++count));
            try{
                Thread.sleep(1000);
            }catch(InterruptedException e){
                e.printStackTrace();
            }
            if (count == 6){
                break;
            }
        }
    }
}
​
class T2 implements Runnable{
    int count = 0;
    @Override
    public void run() {
        while (true){
            System.out.println("hello" + (++count));
            try{
                Thread.sleep(1000);
            }catch(InterruptedException e){
                e.printStackTrace();
            }
            if (count == 6){
                break;
            }
        }
    }
}

 

(3)通过 Callable 和 Future 创建线程
class CallableTest {
    public static void main(String[] args) {
        ThreadTest threadTest = new ThreadTest();
        FutureTask<Integer> futureTask = new FutureTask<>(threadTest);
        Thread thread = new Thread(futureTask);
        thread.start();
        try {
            Integer integer = futureTask.get();
            System.out.println(integer);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException ex) {
            ex.printStackTrace();
        }
    }
    
​
static class ThreadTest implements Callable<Integer> {
    @Override
    public Integer call() throws Exception {
        int i = 0;
        for (; i < 8; i++) {
            System.out.println(Thread.currentThread().getName() + " " + i);
        }
        return i;
    }
}
}

 

(4)通过线程池创建
class CallableTest {
    public static void main(String[] args) {
        ThreadTest threadTest = new ThreadTest();
        FutureTask<Integer> futureTask = new FutureTask<>(threadTest);
        Thread thread = new Thread(futureTask);
        thread.start();
        try {
            Integer integer = futureTask.get();
            System.out.println(integer);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException ex) {
            ex.printStackTrace();
        }
    }
    
​
static class ThreadTest implements Callable<Integer> {
    @Override
    public Integer call() throws Exception {
        int i = 0;
        for (; i < 8; i++) {
            System.out.println(Thread.currentThread().getName() + " " + i);
        }
        return i;
    }
}
}

 

参考地址:https://blog.csdn.net/q66562636/article/details/124825404

posted on 2022-07-21 10:05  yamlln  阅读(69)  评论(0)    收藏  举报