zoukankan      html  css  js  c++  java
  • [Java 09 多线程] 线程是指一个进程在执行过程中可以产生更小的程序单元

    p9 第九章 多线程
      进程是程序的一次性动态执行过程,它需要经历从代码加载,代码执行,到执行完毕的一个完整过程,这个过程也是进程本身从产生,发展到最终消亡的过程。多进程操作系统能同时运行多个进程(程序)。
      多线程是实现并发机制的一种有效手段。进程和线程一样,都是实现并发的一个基本单位。线程是比进程更小的执行单位,线程是在进程的基础上进行的进一步划分。所谓多线程是指一个进程在执行过程中可以产生更小的程序单元,这些更小的单元称为线程,这些线程同时存在,同时运行,一个进程可能包含了多个同时执行的线程。
      Java 中线程的实现
       一,继承 Thread 类
       二,Runnable 接口
       
       public class Thread extends Object implements Runnable
     (1), 线程的中断,合并,后台线程, 线程的休眠, 线程的优先级, 线程的礼让
     (2), 线程的同步 与 死锁
     (3), Object 类对线程的支持 -- 等待与唤醒
     (4), 线程的生命周期
     总结 : Thread / Runnable  run() 方法

            在每一个线程创建和消亡之前,均会处于创建,就绪,运行,阻塞,终止 状态之一

    TestThread1 入门

    package com.qunar.basicJava.javase.p9thread;
    
    /**
     * Author: libin.chen@qunar.com  Date: 14-5-20 14:17
     */
    public class TestThread1 {
        public static void main(String args[]) {
            Runner1 r = new Runner1();
            r.start();
            // r.run();
            // Thread t = new Thread(r);
            // t.start();
    
            for (int i = 0; i < 100; i++) {
                System.out.println("Main Thread:------" + i);
            }
        }
    }
    
    // class Runner1 implements Runnable {
    class Runner1 extends Thread {
        public void run() {
            for (int i = 0; i < 100; i++) {
                System.out.println("Runner1 :" + i);
            }
        }
    }
    // 马老师心得 : 能使用接口,就尽量使用接口,别从 Thread 继承,因为缺少灵活性。
    TestInterrupt 线程的中断
    package com.qunar.basicJava.javase.p9thread;
    
    /**
     * Author: libin.chen@qunar.com  Date: 14-5-20 14:19
     */
    import java.util.*;
    
    public class TestInterrupt {
        public static void main(String[] args) {
            MyThread thread = new MyThread();
            thread.start();
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
            }
            thread.interrupt();
        }
    }
    
    class MyThread extends Thread {
        boolean flag = true;
    
        public void run() {
            while (flag) {
                System.out.println("===" + new Date() + "===");
                try {
                    sleep(1000);
                } catch (InterruptedException e) {
                    return;
                }
            }
        }
    }
    TestJoin  线程的合并
    package com.qunar.basicJava.javase.p9thread;
    
    /**
     * Author: libin.chen@qunar.com  Date: 14-5-20 14:22
     */
    public class TestJoin {
        public static void main(String[] args) {
            MyThread2 t1 = new MyThread2("abcde");
            t1.start();
            try {
                t1.join();         // 合并线程,等待该线程结束,再恢复当前线程的执行
            } catch (InterruptedException e) {
            }
    
    
            for (int i = 1; i <= 10; i++) {
                System.out.println("i am main thread");
            }
        }
    }
    
    
    class MyThread2 extends Thread {
        MyThread2(String s) {
            super(s);
        }
    
    
        public void run() {
            for (int i = 1; i <= 10; i++) {
                System.out.println("i am " + this.getName());  // 线程名字
                try {
                    sleep(1000);
                } catch (InterruptedException e) {
                    return;
                }
            }
        }
    }
    
    TestYield 线程的礼让
    package com.qunar.basicJava.javase.p9thread;
    
    /**
     * Author: libin.chen@qunar.com  Date: 14-5-20 14:27
     */
    public class TestYield {
        public static void main(String[] args) {
            MyThread3 t1 = new MyThread3("t1");
            MyThread3 t2 = new MyThread3("t2");
            t1.start();
            t2.start();
        }
    }
    
    
    class MyThread3 extends Thread {
        MyThread3(String s) {
            super(s);
        }
    
    
        public void run() {
            for (int i = 1; i <= 30; i++) {
                System.out.println(getName() + ": " + i);
                if (i % 10 == 0) {
                    yield();     // 让出 CPU, 让当前线程进入队列等待
                }
            }
        }
    }
    
    TestSync 线程的同步
    package com.qunar.basicJava.javase.p9thread;
    
    /**
     * Author: libin.chen@qunar.com  Date: 14-5-20 14:40
     */
    public class TestSync implements Runnable {
        Timer timer = new Timer();
    
        public static void main(String[] args) {
            TestSync test = new TestSync();
            Thread t1 = new Thread(test);
            Thread t2 = new Thread(test);
            t1.setName("t1");
            t2.setName("t2");
            t1.start();
            t2.start();
        }
    
        public void run() {
            timer.add(Thread.currentThread().getName());
        }
    }
    
    class Timer {
        private static int num = 0;
    
        public synchronized void add(String name) { // 锁定当前对象资源
            // synchronized (this) {
            num++;
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
            }
            System.out.println(name + ", 你是第" + num + "个使用timer的线程");
            // }
        }
    }

    Thread-05- 线程同步-生产者与消费者的经典问题
    package com.bjsxt.chap9Thread;
    
    public class ProducerConsumer {
        public static void main(String[] args) {
            SyncStack ss = new SyncStack();
            Producer p = new Producer(ss);
            Consumer c = new Consumer(ss);
            new Thread(p).start();
            //new Thread(p).start();
            //new Thread(p).start();
            new Thread(c).start();
        }
    }
    
    class WoTou {
        int id;
    
        WoTou(int id) {
            this.id = id;
        }
    
        public String toString() {
            return "WoTou : " + id;
        }
    }
    
    class SyncStack {
        int index = 0;
        WoTou[] arrWT = new WoTou[6];
    
        public synchronized void push(WoTou wt) {
            while (index == arrWT.length) {
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            this.notifyAll();
            arrWT[index] = wt;
            index++;
        }
    
        public synchronized WoTou pop() {
            while (index == 0) {
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            this.notifyAll();
            index--;
            return arrWT[index];
        }
    }
    
    class Producer implements Runnable {
        SyncStack ss = null;
    
        Producer(SyncStack ss) {
            this.ss = ss;
        }
    
        public void run() {
            for (int i = 0; i < 20; i++) {
                WoTou wt = new WoTou(i);
                ss.push(wt);
                System.out.println("生产了:" + wt);
                try {
                    Thread.sleep((int) (Math.random() * 200));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    class Consumer implements Runnable {
        SyncStack ss = null;
    
        Consumer(SyncStack ss) {
            this.ss = ss;
        }
    
        public void run() {
            for (int i = 0; i < 20; i++) {
                WoTou wt = ss.pop();
                System.out.println("消费了: " + wt);
                try {
                    Thread.sleep((int) (Math.random() * 1000));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    





  • 相关阅读:
    Java实现 LeetCode 833 字符串中的查找与替换(暴力模拟)
    Java实现 LeetCode 833 字符串中的查找与替换(暴力模拟)
    Java实现 LeetCode 833 字符串中的查找与替换(暴力模拟)
    Java实现 LeetCode 832 翻转图像(位运算)
    Java实现 LeetCode 832 翻转图像(位运算)
    Java实现 LeetCode 832 翻转图像(位运算)
    Java实现 LeetCode 831 隐藏个人信息(暴力)
    Java实现 LeetCode 831 隐藏个人信息(暴力)
    Java实现 LeetCode 831 隐藏个人信息(暴力)
    how to use automapper in c#, from cf~
  • 原文地址:https://www.cnblogs.com/robbychan/p/3786508.html
Copyright © 2011-2022 走看看