zoukankan      html  css  js  c++  java
  • java_线程

    线程1
        与线程相关的概念
        线程与进程的区别
        线程创建策略
        线程组
        
        线程创建策略
            并发应用中一般有两种不同的线程创建策略
            1直接控制线程的创建和管理
            2将线程的管理从应用程序中抽象出来作为执行器,应用程序将任务传递给执行器,由执行器负责执行。
    线程2
        在线程中实现自己的代码逻辑
        
        有3种方式
            从Thread类继承
            实现runnable接口
            使用方法引用到一个无参数无返回值的方法
            
        将参数传入线程中
            使用线程类属性的set方法
            使用线程类的构造方法传值

    package java20180203_1;
    
    class Thread1 extends Thread {
        private int num;
        public void setNum(int num) {
            this.num = num;
        }
        @Override
        public void run() {
            for (int i = 0; i < 100; i++) {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("thread1:" + i+","+num);
            }
        }
    
    }
    
    class Thread2 implements Runnable {
        private int num;
        public Thread2(int num) {
            this.num = num;
        }
        @Override
        public void run() {
            for (int i = 0; i < 100; i++) {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("thread2:" + i+","+num);
            }
        }
    }
    
    public class ThreadDemo {
    
        static void print(){
            for (int i = 0; i < 100; i++) {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("thread4:" + i);
            }
        }
        public static void main(String[] args) throws Exception {
            /**
             * 线程组,将线程加入到线程组
             */
            Thread t1 = new Thread();
            t1.start();
    
            ThreadGroup tg1 = new ThreadGroup("ThreadGroup");
            Thread t2 = new Thread(tg1, "t2");
    
            /**
             * 从Thread类继承,这是子线程1
             */
            Thread1 th1 = new Thread1();
            //使用线程类set属性进行传值
            th1.setNum(200);
            th1.start();
    
            /**
             * 实现runnable接口,这是子线程2
             */
            //使用构造方法传值
            Runnable r2 = new Thread2(300);
            Thread t3 = new Thread(r2);
            t3.start();
    
            /**
             * 用lambda表达式来实现相同的功能,这是子线程3
             */
            new Thread(() -> {
                for (int i = 0; i < 100; i++) {
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("thread3:" + i);
                }
            }).start();
            
            /**
             * 方法引用来创建线程
             */
            new Thread(ThreadDemo::print).start();
            
            /**
             * 这是main线程的循环
             * 总共有4个线程,一个是main主线程的循环,其余3个是子线程的循环,有3个start,它们是并行的,可以从结果中看到。
             * 分发给不同的处理器来执行
             */
            for (int i = 0; i < 100; i++) {
                Thread.sleep(500);
                System.out.println("main:" + i);
            }
        }
    }

    线程3
        获取线程的信息
        线程优先级
        线程串行化
        线程休眠
        
        获取线程的信息
            currentThread()
            getName
        优先级
            默认优先级是5,子默认与父优先级相同,共10个,从1到10。
        串行化
            若一个线程运行的过程中要用到另一个线程的结果,则可以进行线程的串行化处理
            join()方法
        休眠
            sleep()方法,暂停时间不精确

            
    线程4
        线程的状态
        线程中断
        线程停止
        守护线程
        
        
        线程的状态
            可以使用getState()来获取,有New,Runnable,Blocked,Waiting,ThreadWaiting,Dead这6种状态

        线程停止
            开发中使线程停止的方式不是使用其stop(),已经不推荐使用了
            而是使用标识量来停止线程

    package java20180205;
    
    public class InterruptDemo {
    
        public static void main(String[] args) throws Exception {
            Thread t1=new Thread(()->{
                for (int i = 0; i < 20; i++) {
                    System.out.println(i);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
            t1.start();
            for (int i = 0; i < 20; i++) {
                System.out.println("main:"+i);
                Thread.sleep(300);
            }
            t1.interrupt();
            /**
             * 一个是false,一个是true
             */
    //        System.out.println(t1.interrupted());
    //        System.out.println(t1.isInterrupted());
        }
    
    }
    package java20180205;
    
    class Thread4 implements Runnable{
    
        private boolean stop=false;
        public void setStop(boolean stop) {
            this.stop = stop;
        }
        
        @Override
        public void run() {
            for (int i = 0;; i++) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (stop) {
                    break;
                }
                System.out.println(i);
            }
        }
    }
    
    public class StopDemo {
    
        public static void main(String[] args) throws Exception {
    //        Thread4 t4=new Thread4();
    //        new Thread(t4).start();
    //        Thread.sleep(4000);
    //        t4.setStop(true);
            
            
            Thread4 t4=new Thread4();
            Thread t5=new Thread(t4);
            t5.setDaemon(true);
            t5.start();
            Thread.sleep(4000);
            
        }
    }

    线程5
        多线程访问共享数据的问题
        java内存模型
            
        案例
            使用多个线程调用一个账号实例中的取款方法
        java内存模型
            线程安全术语

    线程6
        对象监视器
        synchronized关键字
        volatile变量
        
        对象监视器
            临界区,监视器上加锁
        
        synchronized关键字
            用于声明临界区
            两种方法:
                synchronized方法
                    实例方法:锁加在实例上
                    静态方法:锁加在类上
                synchronized块
            
    线程7    05:29
        生产者消费者模型
        wait与sleep的区别
        
        生产者消费者模型
            线程间通信的另一种方式,是一个非常经典的模型

  • 相关阅读:
    大数据的前景?
    PriorityBlockingQueue深度解析(好文)
    深入CAS原理
    common-lang3工具类-使用手册
    gitlab搭建
    RestTemplate转码bug
    论tigergraph边的方向性
    关于java的wait方法的深入分析
    openjdk的源码下载方式
    一个奇怪的urlencode转码问题
  • 原文地址:https://www.cnblogs.com/createyuan/p/8410567.html
Copyright © 2011-2022 走看看