美文网首页
进程/多线程

进程/多线程

作者: 帅哥_刷哥 | 来源:发表于2017-10-10 23:43 被阅读12次

进程与线程

进程
    系统运行程序的基本单位
    有独立的内存空间和系统资源
线程
    进程中执行运算的最小单位
    处理机分配给线程,即真正在处理机上运行的是线程

线程

创建线程的两种传统方式
    继承Thread类
        MyThread thread = new MyThread();
        thread.start();//启动线程
    实现Runnable接口
        MyRunnable runnable = new MyRunnable();
        new Thread(runnable).start();   
API
    Thread 线程类
        getName()
            获得当前线程的名称
        currentThread()
            获得当前线程
        sleep()
            让线程的睡眠指定的时间 
多线程会提高程序的运行效率吗?
    不会,性能会降低。
为什么多线程下载会速度快?
    其实是在抢服务器的带宽。

创建定时器

定时器
    1.Timer类与TimerTask类

//5秒钟后调度执行任务
//只执行一次
//相同一个定时器只能用一次,否则会报错
Timer timer = new Timer();
timer.schedule(new TimerTask() {
    @Override
    public void run() {
        //任务
    }
}, 5*1000);

//5秒钟后调度执行任务,以后每2秒再执行一次
Timer timer = new Timer();
//5秒钟后调度任务
timer.schedule(new TimerTask() {
    @Override
    public void run() {
        //任务
    }
}, 5*1000,2*1000);  

线程同步synchronized

1.同步代码块
    synchronized (this) {
        
    }
    
2.同步方法
    public synchronized void output(){}
    锁对象是当前Java类的字节码对象 Test.class

线程通信-wait/notify

生产者与消费者

Lock&Condition

Lock比传统线程模型中的synchronized方式更加面向对象,与生活中的锁类似。
锁本身也应该是一个对象。两个线程执行的代码片段要实现同步互斥的效果,他们必须
用同一个Lock对象,锁是代表要操作的资源的类的内部方法中,而不是线程线程代码中。

线程范围内的共享数据

//有问题,打印的数据不对
    public class ThreadShareData1 {
        private static int data = 0;
        static class A{
            public void get(){
                System.out.println(Thread.currentThread().getName()+" a:"+data);
            }
        }
        static class B{
            public void get(){
                System.out.println(Thread.currentThread().getName()+" b:"+data);
            }
        }
        public static void main(String[] args) {
            for(int i = 0;i<5;i++){
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        data = new Random().nextInt(100);
                        System.out.println(Thread.currentThread().getName()+" input data :"+data);
                        new A().get();
                        new B().get();
                    }
                }).start();
            }
        }
    }
//在某一个线程内,有独立的数据
    public class ThreadShareData {
        private static Map<Thread,Integer> threadData = new HashMap<Thread,Integer>();
        static class A{
            public void get(){
                int data = threadData.get(Thread.currentThread());
                System.out.println(Thread.currentThread().getName()+" a:"+data);
            }
        }
        static class B{
            public void get(){
                int data = threadData.get(Thread.currentThread());
                System.out.println(Thread.currentThread().getName()+" b:"+data);
            }
        }
        public static void main(String[] args) {
            for(int i = 0;i<5;i++){
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        int data = new Random().nextInt(100);
                        System.out.println(Thread.currentThread().getName()+" input data :"+data);
                        threadData.put(Thread.currentThread(), data);
                        new A().get();
                        new B().get();
                    }
                }).start();
            }
        }
    }
//Java提供的类,解决线程数据共享问题
//在某一个线程内,有独立的数据
//总结:一个ThreadLocal只能代表一个变量,所以其中只能放一个数据。如果你有两个变量,只能创建两个ThreadLocal对象。
//也可以把一堆数据封装到一个对象中。
    public class ThreadShareData {
        //相当于往map中放整数
        private static ThreadLocal<Integer> tl = new ThreadLocal<Integer>();
        static class A{
            public void get(){
                int data = tl.get();
                System.out.println(Thread.currentThread().getName()+" a:"+data);
            }
        }
        static class B{
            public void get(){
                int data = tl.get();
                System.out.println(Thread.currentThread().getName()+" b:"+data);
            }
        }
        public static void main(String[] args) {
            for(int i = 0;i<5;i++){
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        int data = new Random().nextInt(100);
                        System.out.println(Thread.currentThread().getName()+" input data :"+data);
                        tl.set(data);
                        new A().get();
                        new B().get();
                    }
                }).start();
            }
        }
    }
//在某一个线程内,有独立的数据,封装多个数据
    class User{
        private int id;
        private String name;
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
    }
    public class ThreadShareData {
        //相当于往map中放整数
        private static ThreadLocal<User> tl = new ThreadLocal<User>();
        static class A{
            public void get(){
                User user = tl.get();
                System.out.println(Thread.currentThread().getName()+" a:"+user.getName());
            }
        }
        static class B{
            public void get(){
                User user = tl.get();
                System.out.println(Thread.currentThread().getName()+" b:"+user.getName());
            }
        }
        public static void main(String[] args) {
            for(int i = 0;i<2;i++){
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        int data = new Random().nextInt(100);
                        System.out.println(Thread.currentThread().getName()+" input data :"+data);
                        User user = new User();
                        user.setId(data);
                        user.setName("user"+data);
                        tl.set(user);
                        new A().get();
                        new B().get();
                    }
                }).start();
            }
        }
    }
优美设计
    public class Data {
        private Data(){}
        /**
         * 每个线程调用全局的ThreadLocal对象的set方法,就相当于往其内部的map对象中添加一条记录。
         * key分别是各自的线程,value是传入的值。
         * 在线程结束的时候可以调用ThreadLocal的clear方法,这样可以快速释放内存。不调用也可以,在线程结束的时候自动释放。
         * */
        private static ThreadLocal<Data> map = new ThreadLocal<Data>();
        public static Data getInstance(){//这里不需要synchronized
            Data data = map.get();
            if(data == null){
                data = new Data();
                map.set(data);
            }
            return data;
        }
        private int id;
        private String name;
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
    }
    public class ThreadShareData {
        //相当于往map中放整数
        static class A{
            public void get(){
                System.out.println(Thread.currentThread().getName()+" a:"+Data.getInstance().getId());
            }
        }
        static class B{
            public void get(){
                System.out.println(Thread.currentThread().getName()+" b:"+Data.getInstance().getId());
            }
        }
        public static void main(String[] args) {
            for(int i = 0;i<2;i++){
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        int data = new Random().nextInt(100);
                        System.out.println(Thread.currentThread().getName()+" input data :"+data);
                        Data.getInstance().setId(data);
                        new A().get();
                        new B().get();
                    }
                }).start();
            }
        }
    }
多个线程访问共享对象和数据的方式
    可以使用同一个Runnable对象,这样就可以操作同一个数据了

相关文章

  • 多线程介绍

    一、进程与线程 进程介绍 线程介绍 线程的串行 二、多线程 多线程介绍 多线程原理 多线程的优缺点 多线程优点: ...

  • 多进程和多线程编程

    多任务的实现方式: 多进程模式 多线程模式 多进程 + 多线程 模式python即支持多进程,又支持多线程,如下进...

  • 【Java 基础你一定要掌握的知识点】多线程

    Java 给多线程编程提供了内置的支持。在多线程编程之前,我们需要先了解什么是线程。 进程和多线程简介 进程:进程...

  • 多线程及网络编程

    多线程1.多线程的概述: 进程有多条执行路径,合称为多线程.进程:可执行文件(程序).eg:exe文件线程:进程的...

  • Java 多线程

    1 多线程 1.1 多线程介绍   学习多线程之前,我们先要了解几个关于多线程有关的概念。  进程:进程指正在运行...

  • [CP_12] Python多线程爬虫应用实践(社招职位名称获取

    目录结构 一、多线程爬虫的使用 1. 多线程实现 <关联> [Pt_04] Python进程|多进程|线程|多线程...

  • Java多线程基础学习

    Java多线程基础 1.多线程简介 在了解多线程之前我们要先知道什么是进程和线程: 进程:进程是系统进行调度和分配...

  • IOS2

    一、进程和线程:什么是进程? 什么是线程? 多线程原理? 二、多线程 iOS中多线程实现方案: 1.pthread...

  • 2019 -----进程、线程、多进程、多线程、任务、队列、NS

    进程 线程 进程和线程的关系多进程 多线程 任务 队列 iOS 中的多线程 一、进程 1.进程是一个具有一定独立功...

  • 网络爬虫:多任务-进程、线程

    实现多任务的方式 多线程多进程协程多线程+多进程 为什么你能够实现多任务? 并行:同时发起,同时执行,多进程,进程...

网友评论

      本文标题:进程/多线程

      本文链接:https://www.haomeiwen.com/subject/kkypextx.html