多线程学习笔记

时间:2020-02-24 18:56:42   收藏:0   阅读:63

多线程学习笔记

简介

程序,进程,线程

普通方法与多线程

Process与Thread

核心概念

线程创建

Thread

package com.taidou;

//继承Thread类
public class TestThread extends Thread{
    //重写run方法
    @Override
    public void run() {
        for (int i = 0; i < 200; i++) {
            System.out.println("Thread线程"+i);
        }
    }

    //main线程,主线程
    public static void main(String[] args) {
        //创建线程对象,调用start方法启动线程
        TestThread thread = new TestThread();
        thread.start();

        //main方法体
        for (int i = 0; i < 200; i++) {
            System.out.println("main线程"+i);
        }
    }
}

技术分享图片

Runnable

package com.taidou;

//继承Thread类
public class TestThread2 implements Runnable{
    //重写run方法
    @Override
    public void run() {
        for (int i = 0; i < 200; i++) {
            System.out.println("Thread线程"+i);
        }
    }

    //main线程,主线程
    public static void main(String[] args) {
        //创建Runnable接口的实现类对象
        TestThread2 thread2 = new TestThread2();
        //创建线程对象,调用start方法启动线程,代理
        //Thread thread = new Thread(thread2);
        //thread.start();
        new Thread(thread2).start();

        //main方法体
        for (int i = 0; i < 200; i++) {
            System.out.println("main线程"+i);
        }
    }
}

Thread与Runnable

package com.taidou;


//多个线程操作同一对象
//买票
public class TestThread3 implements Runnable{
    //票数
    private int tickeNums =10;

    @Override
    public void run() {
        buy();
    }


    //买票的方法
    private void buy() {
        while (true){
            if (tickeNums<=0){
                break;
            }
            //模拟延时
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"买到了倒数第" + tickeNums-- + "张票");
        }
    }



    //main线程,主线程
    public static void main(String[] args) {
        TestThread3 ticke = new TestThread3();

        new Thread(ticke,"小明").start();
        new Thread(ticke,"小红").start();
        new Thread(ticke,"小刚").start();


    }


}

Callable(了解)

线程状态

停止线程

线程休眠

线程礼让

join

线程状态观测

线程优先级

守护(daemon)线程

线程同步

队列和锁

线程不安全的例子

package com.taidou;


//多个线程操作同一对象
//买票
public class TestThread3 implements Runnable{
    //票数
    private int tickeNums =10;

    @Override
    public void run() {
        buy();
    }


    //买票的方法
    private void buy() {
        while (true){
            if (tickeNums<=0){
                break;
            }
            //模拟延时
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"买到了倒数第" + tickeNums-- + "张票");
        }
    }



    //main线程,主线程
    public static void main(String[] args) {
        TestThread3 ticke = new TestThread3();

        new Thread(ticke,"小明").start();
        new Thread(ticke,"小红").start();
        new Thread(ticke,"小刚").start();


    }


}

技术分享图片

同步方法

package com.taidou;


//多个线程操作同一对象
//买票
public class TestThread4 implements Runnable{
    //票数
    private int tickeNums =10;

    @Override
    public void run() {
        buy();
    }


    //买票的方法
    private synchronized void buy() {
        while (true){
            if (tickeNums<=0){
                break;
            }
            //模拟延时
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"买到了倒数第" + tickeNums-- + "张票");
        }
    }



    //main线程,主线程
    public static void main(String[] args) {
        TestThread4 ticke = new TestThread4();

        new Thread(ticke,"小明").start();
        new Thread(ticke,"小红").start();
        new Thread(ticke,"小刚").start();


    }


}

技术分享图片

同步块

死锁

Lock(锁)

synchronized与Lock

线程协作

生产者消费者模式

线程通信

解决方法

线程池

原文:https://www.cnblogs.com/taidou-hth/p/12358187.html

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