并发

【Java】【Thread多线程】概述

2017-04-08  本文已影响67人  JerichoPH

多线程概述

  1. 通过子类继承开启多线程
    public class Demo {
        public static void main(String[] args) throws IOException {
        MyThread myThread = new MyThread();
        myThread.start();
        
        for(int i = 0;i<1000;i++){
            System.out.print("b");
        }
    }
    }
    
    class MyThread extends Thread {
        public void run() {
            for (int i = 0; i < 1000; i++) {
                System.out.print("a");
            }
        }
    }
    
  2. 通过实现Runnable接口开启多线程
    public class Demo {
        public static void main(String[] args) throws IOException {
            MyThread myThread = new MyThread();
            Thread t = new Thread(myThread);
            t.start();
            
            for (int i = 0; i < 1000; i++) {
                System.out.print("b");
            }
        }
    }
    
    class MyThread implements Runnable {
        @Override
        public void run() {
            for (int i = 0; i < 1000; i++) {
                System.out.print("a");
            }
        }
    }
    
  3. 通过匿名内部类实现多线程
    // 方法1
    public static void main(String[] args) throws IOException {
        new Thread(){
            @Override
            public void run() {
                for(int i = 0;i<1000;i++){
                    System.out.print("a");
                }
            }
        }.start();
        for(int i = 0;i<1000;i++){
            System.out.print("b");
        }
    }
    // 方法2
    public static void main(String[] args) throws IOException {
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 1000; i++) {
                    System.out.print("a");
                }
            }
        }) {
        }.start();
        for (int i = 0; i < 1000; i++) {
            System.out.print("b");
        }
    }
    
  4. 设置和获取线程名称(setName&getName)
    public class Demo {
        public static void main(String[] args) throws IOException {
            new Thread("subThread A") { // 通过构造方法修改名称
                @Override
                public void run() {
                    for (int i = 0; i < 1000; i++) {
                        System.out.println(this.getName());
                    }
                }
            }.start();
            
            new Thread() {
                @Override
                public void run() {
                    this.setName("subThread B"); // 通过setName方法 修改线程名称
                    for (int i = 0; i < 1000; i++) {
                        System.out.println(this.getName());
                    }
                }
            }.start();
        }
    }
    
  5. 获取当前线程对象(Thread.currentThread)
    public class Demo {
        public static void main(String[] args) throws IOException {
            new Thread("subThread A") { // 通过构造方法修改名称
                @Override
                public void run() {
                    for (int i = 0; i < 1000; i++) {
                        System.out.println(this.getName());
                    }
                }
            }.start();
            
            new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int i = 0; i < 1000; i++) {
                        System.out.print(Thread.currentThread().getName()); // 通过Runnable方法实现的多线程,无法通过this获取Thread类对象,只能通过Thread.currentThread获取当前线程
                    }
                }
            }) {
            }.start();
            
            // 通过Thread.currentThread方法获取和设置主线程属性
            Thread.currentThread().setName("我是主线程");
            System.out.println(Thread.currentThread().getName());
        }
    }
    
  6. 线程休眠(sleep)
    public class Demo {
        public static void main(String[] args) throws IOException {
            new Thread() {
                @Override
                public void run() {
                    for (int i = 0; i < 10; i++) {
                        System.out.println(new MyDatetime(System.currentTimeMillis()).getCn());
                        try { // 每次线程执行一次则休眠一秒
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }.start();
        }
    }
    
  7. 守护线程(setDaemon)
    public class Demo {
        public static void main(String[] args) throws IOException {
            Thread t1 = new Thread() { // 这里是守护线程
                @Override
                public void run() {
                    for (int i = 0; i < 1000; i++) {
                        System.out.print("a"); // 执行1000次打印
                    }
                }
            };
            
            Thread t2 = new Thread() {
                @Override
                public void run() {
                    for (int i = 0; i < 5; i++) {
                        System.out.print("b"); // 执行5次打印
                    }
                }
            };
            
            t1.setDaemon(true);
            
            t1.start();
            t2.start();
            // 结论:当正常线程结束后,守护线程跟随停止
        }
    }
    
  8. 加入线程(join)
    public class Demo {
        public static void main(String[] args) throws IOException {
            final Thread t1 = new Thread() { // 匿名内部类在使用其方法的时候需要使用final修饰
                @Override
                public void run() {
                    for (int i = 0; i < 10; i++) {
                        System.out.print("a");
                        try {
                            Thread.sleep(10);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            };
            Thread t2 = new Thread() {
                @Override
                public void run() {
                    for (int i = 0; i < 10; i++) {
                        if (i == 2) {
                            try {
                                t1.join();
                                Thread.sleep(10);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        System.out.print("b");
                    }
                }
            };
            t1.start();
            t2.start();
            // 执行结果:abbaaaaaaaaabbbbbbb
        }
    }
    
    public class Demo {
        public static void main(String[] args) throws IOException {
            final Thread t1 = new Thread() { // 匿名内部类在使用其方法的时候需要使用final修饰
                @Override
                public void run() {
                    for (int i = 0; i < 10; i++) {
                        System.out.print("a");
                        try {
                            Thread.sleep(10);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            };
            Thread t2 = new Thread() {
                @Override
                public void run() {
                    for (int i = 0; i < 10; i++) {
                        if (i == 2) {
                            try {
                                t1.join(10); // 当b线程执行完成后,交还给a执行10毫秒,再次交替执行
                                Thread.sleep(10);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        System.out.print("b");
                    }
                }
            };
            t1.start();
            t2.start();
            // 执行结果:abbaabbbbbbbbaaaaaaa
        }
    }
    
  9. 同步代码块(synchronized)
    public class Demo {
        public static void main(String[] args) throws IOException {
            final Person person = new Person();
            new Thread() {
                @Override
                public void run() {
                    for (int i = 0; i <= 100; i++) {
                        person.printNum();
                        System.out.println();
                    }
                }
            }.start();
            new Thread() {
                @Override
                public void run() {
                    for (int i = 0; i < 100; i++) {
                        person.printLetter();
                        System.out.println();
                    }
                }
            }.start();
        }
    }
    
    class Person {
        public void printNum() {
            // 强制程序执行时,执行该代码块完全执行后,再跳转其他线程。同步代码块
            synchronized (this) {
                System.out.print("1");
                System.out.print("2");
                System.out.print("3");
                System.out.print("4");
                System.out.print("5");
            }
        }
        
        public void printLetter() {
            synchronized (this) {
                System.out.print("a");
                System.out.print("b");
                System.out.print("c");
                System.out.print("d");
                System.out.print("e");
            }
        }
    }
    
  10. 同步方法
    public class Demo {
        public static void main(String[] args) throws IOException {
            final Person person = new Person();
            new Thread() {
                @Override
                public void run() {
                    for (int i = 0; i <= 100; i++) {
                        person.printNum();
                        System.out.println();
                    }
                }
            }.start();
            new Thread() {
                @Override
                public void run() {
                    for (int i = 0; i < 100; i++) {
                        person.printLetter();
                        System.out.println();
                    }
                }
            }.start();
        }
    }
    
    class Person {
    // 同步方法
        public synchronized void printNum() {
            System.out.print("1");
            System.out.print("2");
            System.out.print("3");
            System.out.print("4");
            System.out.print("5");
        }
        
        public void printLetter() {
            synchronized (this) {
                System.out.print("a");
                System.out.print("b");
                System.out.print("c");
                System.out.print("d");
                System.out.print("e");
            }
        }
    
    // 静态同步方法
        public static synchronized void printNum2() {
            System.out.print("1");
            System.out.print("2");
            System.out.print("3");
            System.out.print("4");
            System.out.print("5");
        }
        
        public static void printLetter2() {
            synchronized (Person.class) {
                System.out.print("a");
                System.out.print("b");
                System.out.print("c");
                System.out.print("d");
                System.out.print("e");
            }
        }
    }
    
  11. 多线程安全
    public class Demo {
        public static void main(String[] args) throws IOException {
            // 同时打开4个售票窗口进行售票
            new Ticket().start();
            new Ticket().start();
            new Ticket().start();
            new Ticket().start();
        }
    }
    
    // 售票窗口
    class Ticket extends Thread {
        private static int ticket = 100;
        
        @Override
        public void run() {
            while (true) {
                synchronized (Ticket.class) { // 必须使用静态
                    if (ticket <= 0) {
                        // 防止出现0号或负号票
                        break;
                    }
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("第" + ticket-- + "张票");
                }
            }
        }
    }
    
  12. 多线程安全(死锁)
    public class Demo {
    
        private static String s1 = "《筷子左》";
        private static String s2 = "《筷子右》";
        
        public static void main(String[] args) throws IOException {
            // 使用者1
            new Thread("使用者1") {
                @Override
                public void run() {
                    while(true){
                        synchronized (s1) {
                            System.out.println(this.getName() + ":拿到" + s1 + "等待" + s2);
                            synchronized (s2) {
                                System.out.println(this.getName() + ":拿到" + s2 + "开吃");
                            }
                        }
                    }
                }
            }.start();
            
            // 使用者2
            new Thread("使用者2") {
                @Override
                public void run() {
                    while(true){
                        synchronized (s2) {
                            System.out.println(this.getName() + ":拿到" + s2 + "等待" + s1);
                            synchronized (s1) {
                                System.out.println(this.getName() + ":拿到" + s1 + "开吃");
                            }
                        }
                    }
                }
            }.start();
        }
    }
    
上一篇下一篇

猜你喜欢

热点阅读