Java中的多线程基础

时间:2019-10-07 17:49:21   收藏:0   阅读:97

1、线程与进程

进程:

 技术分享图片

线程:

2、并发和并行

并发:

并行:

3、线程的上下文切换

基本原理:

4、线程的几种状态

基本原理:

5、创建线程的几种方式

继承Thread类:

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

class TestThread extends Thread{
    @Override
    public void run(){
        System.out.println("Test Thread");
    }
}

实现Runnable接口:

public class ThreadTest {
    public static void main(String[] args){
        new Thread(new TestRunnable()).start();
    }
}

class TestRunnable implements Runnable{
    @Override
    public void run() {
        System.out.println("Test Runnable");
    }
}

实现Callable类:

public class ThreadTest {
    public static void main(String[] args){
        FutureTask<Integer> task = new FutureTask(new TestCallable());
        new Thread(task).start();
        try {
            System.out.println(task.get());//get方法会得到call执行完之后的值
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}

class TestCallable implements Callable<Integer> {
    @Override
    public Integer call() throws Exception {
        int num = 0;
        while(num < 5){
            num++;
        }
        return num;
    }
}

6、sleep() 和 wait()

sleep():

wait():

public class ThreadTest {
    protected static volatile Object lock = "lock";

    public static void main(String[] args){

        for(int i=0;i<5;i++){
            new Thread(() -> {
                synchronized(lock){
                    System.out.println(Thread.currentThread().getName() + ":等待开始当前时间戳:" + System.currentTimeMillis());
                    try {
//                        lock.wait(1000);//让当前线程进入等待池
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }
}

7、关于wait() 与 notify(),notifyAll()

notify(),notifyAll():

public class ThreadTest {
    protected static volatile Object lock = "lock";

    public static void main(String[] args){
        new Thread(new TestRunnable(lock)).start();
        try {
            Thread.sleep(1000);
            System.out.println("sleep 1000ms");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new TestThread(lock).start();

    }
}

class TestRunnable implements Runnable{
    private Object lock;
    public TestRunnable(Object lock){
        this.lock = lock;
    }
    @Override
    public void run() {
        try {
            synchronized (lock){
                System.out.println("begin wait:" + System.currentTimeMillis());
                System.out.println("Release lock........");
                lock.wait();//让当前线程进入等待池
                System.out.println("end wait:" + System.currentTimeMillis());
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

class TestThread extends Thread{
    private Object lock;
    public TestThread(Object lock){
        this.lock = lock;
    }
    @Override
    public void run() {
        synchronized (lock){
            System.out.println("begin notify:" + System.currentTimeMillis());
            System.out.println("do something........");
            lock.notify();//从等待池中随机唤醒一个wait线程进入锁池竞争锁
//            lock.notifyAll();//从等待池中随机唤醒所有wait线程进入锁池竞争锁
            System.out.println("end notify:" + System.currentTimeMillis());
        }
    }
}

8、start() 和 run()

start():

run():

9、线程安全性

主要体现在下面三方面:

10、线程死锁

死锁:

public class ThreadTest {
    //共享资源A
    private static Object A = new Object();
    //共享资源B
    private static Object B = new Object();

    public static void main(String[] args){
        new Thread(() -> {
            synchronized (A){
                System.out.println(Thread.currentThread().getName() + ":得到资源---A");
                try {
                    Thread.sleep(1000);//此处休眠是为了让其他线程获得执行机会
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + ":去获取资源---B");
                synchronized (B){
                    System.out.println(Thread.currentThread().getName() + ":得到资源---B");
                }
            }
        },"Thread-01").start();

        new Thread(() -> {
            synchronized (B){
                System.out.println(Thread.currentThread().getName() + ":得到资源---B");
                try {
                    Thread.sleep(1000);//此处休眠是为了让其他线程获得执行机会
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + ":去获取资源---A");
                synchronized (A){
                    System.out.println(Thread.currentThread().getName() + ":得到资源---A");
                }
            }
        },"Thread-02").start();
    }
}

死锁四个必要条件即如何避免死锁:

public class ThreadTest {
//共享资源A
private static Object A = new Object();
//共享资源B
private static Object B = new Object();

public static void main(String[] args){
new Thread(() -> {
synchronized (A){
System.out.println(Thread.currentThread().getName() + ":得到资源---A");
try {
Thread.sleep(1000);//此处休眠是为了让其他线程获得执行机会
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + ":去获取资源---B");
synchronized (B){
System.out.println(Thread.currentThread().getName() + ":得到资源---B");
}
}
},"Thread-01").start();

new Thread(() -> {
//线程2去获取A时被阻塞
synchronized (A){
System.out.println(Thread.currentThread().getName() + ":得到资源---A");
try {
Thread.sleep(1000);//此处休眠是为了让其他线程获得执行机会
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + ":去获取资源---B");
synchronized (B){
System.out.println(Thread.currentThread().getName() + ":得到资源---B");
}
}
},"Thread-02").start();
}
}

  (以上所有内容皆为个人笔记,如有错误之处还望指正。)

原文:https://www.cnblogs.com/xihuantingfeng/p/11628820.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!