多线程(上)

2019-02-18  本文已影响0人  黄同学2019

day24(多线程(上))

1_多线程(多线程的引入)(了解)

2_多线程(多线程并行和并发的区别)(了解)

3_多线程(Java程序运行原理和JVM的启动是多线程的吗)(了解)

4_多线程(多线程程序实现的方式1)(掌握)

            public class Demo2_Thread {
        
                /**
                 * @param args
                 */
                public static void main(String[] args) {
                    MyThread mt = new MyThread();                           //4,创建自定义类的对象
                    mt.start();                                             //5,开启线程
                    
                    for(int i = 0; i < 3000; i++) {
                        System.out.println("bb");
                    }
                }
            
            }
            class MyThread extends Thread {                                 //1,定义类继承Thread
                public void run() {                                         //2,重写run方法
                    for(int i = 0; i < 3000; i++) {                         //3,将要执行的代码,写在run方法中
                        System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaa");
                    }
                }
            }

5_多线程(多线程程序实现的方式2)(掌握)

            public class Demo3_Runnable {
        ​       /**
        ​        * @param args
        ​        */
        ​       public static void main(String[] args) {
        ​           MyRunnable mr = new MyRunnable();                       //4,创建自定义类对象
        ​           //Runnable target = new MyRunnable();
        ​           Thread t = new Thread(mr);                              //5,将其当作参数传递给Thread的构造函数
        ​           t.start();                                              //6,开启线程
        ​           
                    for(int i = 0; i < 3000; i++) {
                        System.out.println("bb");
                    }
                }
            }
            
            class MyRunnable implements Runnable {                          //1,自定义类实现Runnable接口
                @Override
                public void run() {                                         //2,重写run方法
                    for(int i = 0; i < 3000; i++) {                         //3,将要执行的代码,写在run方法中
                        System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaa");
                    }
                }
                
            }

6_多线程(实现Runnable的原理)(了解)

7_多线程(两种方式的区别)(掌握)

8_多线程(匿名内部类实现线程的两种方式)(掌握)

        new Thread() {                                                  //1,new 类(){}继承这个类
    ​       public void run() {                                         //2,重写run方法
    ​           for(int i = 0; i < 3000; i++) {                         //3,将要执行的代码,写在run方法中
    ​               System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaa");
    ​           }
    ​       }
    ​   }.start();
        new Thread(new Runnable(){                                      //1,new 接口(){}实现这个接口
    ​       public void run() {                                         //2,重写run方法
    ​           for(int i = 0; i < 3000; i++) {                         //3,将要执行的代码,写在run方法中
    ​               System.out.println("bb");
    ​           }
    ​       }
    ​   }).start(); 

9_多线程(获取名字和设置名字)(掌握)

            new Thread("xxx") {
        ​       public void run() {
        ​           for(int i = 0; i < 1000; i++) {
        ​               System.out.println(this.getName() + "....aaaaaaaaaaaaaaaaaaaaaaa");
        ​           }
        ​       }
        ​   }.start();
        ​   
            new Thread("yyy") {
                public void run() {
                    for(int i = 0; i < 1000; i++) {
                        System.out.println(this.getName() + "....bb");
                    }
                }
            }.start(); 
            Thread t1 = new Thread() {
        ​       public void run() {
        ​           for(int i = 0; i < 1000; i++) {
        ​               System.out.println(this.getName() + "....aaaaaaaaaaaaaaaaaaaaaaa");
        ​           }
        ​       }
        ​   };
        ​   
            Thread t2 = new Thread() {
                public void run() {
                    for(int i = 0; i < 1000; i++) {
                        System.out.println(this.getName() + "....bb");
                    }
                }
            };
            t1.setName("芙蓉姐姐");
            t2.setName("凤姐");
            
            t1.start();
            t2.start();

10_多线程(获取当前线程的对象)(掌握)

            new Thread(new Runnable() {
        ​       public void run() {
        ​           for(int i = 0; i < 1000; i++) {
        ​               System.out.println(Thread.currentThread().getName() + "...aaaaaaaaaaaaaaaaaaaaa");
        ​           }
        ​       }
        ​   }).start();
        ​   
            new Thread(new Runnable() {
                public void run() {
                    for(int i = 0; i < 1000; i++) {
                        System.out.println(Thread.currentThread().getName() + "...bb");
                    }
                }
            }).start();
            Thread.currentThread().setName("我是主线程");                    //获取主函数线程的引用,并改名字
            System.out.println(Thread.currentThread().getName());       //获取主函数线程的引用,并获取名字

11_多线程(休眠线程)(掌握)

            new Thread() {
    ​           public void run() {
    ​               for(int i = 0; i < 10; i++) {
    ​                   System.out.println(getName() + "...aaaaaaaaaaaaaaaaaaaaaa");
    ​                   try {
    ​                       Thread.sleep(10);
    ​                   } catch (InterruptedException e) {
    ​                       e.printStackTrace();
    ​                   }
    ​               }
    ​           }
    ​       }.start();
    ​       
            new Thread() {
                public void run() {
                    for(int i = 0; i < 10; i++) {
                        System.out.println(getName() + "...bb");
                        try {
                            Thread.sleep(10);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }.start();

12_多线程(守护线程)(掌握)

            Thread t1 = new Thread() {
        ​       public void run() {
        ​           for(int i = 0; i < 50; i++) {
        ​               System.out.println(getName() + "...aaaaaaaaaaaaaaaaaaaaaa");
        ​               try {
        ​                   Thread.sleep(10);
        ​               } catch (InterruptedException e) {
        ​                   e.printStackTrace();
        ​               }
        ​           }
        ​       }
        ​   };
        ​   
            Thread t2 = new Thread() {
                public void run() {
                    for(int i = 0; i < 5; i++) {
                        System.out.println(getName() + "...bb");
                        try {
                            Thread.sleep(10);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            };
            
            t1.setDaemon(true);                     //将t1设置为守护线程
            
            t1.start();
            t2.start();

13_多线程(加入线程)(掌握)

            final Thread t1 = new Thread() {
        ​       public void run() {
        ​           for(int i = 0; i < 50; i++) {
        ​               System.out.println(getName() + "...aaaaaaaaaaaaaaaaaaaaaa");
        ​               try {
        ​                   Thread.sleep(10);
        ​               } catch (InterruptedException e) {
        ​                   e.printStackTrace();
        ​               }
        ​           }
        ​       }
        ​   };
        ​   
            Thread t2 = new Thread() {
                public void run() {
                    for(int i = 0; i < 50; i++) {
                        if(i == 2) {
                            try {
                                //t1.join();                        //插队,加入
                                t1.join(30);                        //加入,有固定的时间,过了固定时间,继续交替执行
                                Thread.sleep(10);
                            } catch (InterruptedException e) {
                                
                                e.printStackTrace();
                            }
                        }
                        System.out.println(getName() + "...bb");
                    
                    }
                }
            };
            
            t1.start();
            t2.start();

14_多线程(礼让线程)(了解)

15_多线程(设置线程的优先级)(了解)

16_多线程(同步代码块)(掌握)

            class Printer {
        ​       Demo d = new Demo();
        ​       public static void print1() {
        ​           synchronized(d){                //锁对象可以是任意对象,但是被锁的代码需要保证是同一把锁,不能用匿名对象
        ​               System.out.print("黑");
        ​               System.out.print("马");
        ​               System.out.print("程");
        ​               System.out.print("序");
        ​               System.out.print("员");
        ​               System.out.print("\r\n");
        ​           }
        ​       }
    
                public static void print2() {   
                    synchronized(d){    
                        System.out.print("传");
                        System.out.print("智");
                        System.out.print("播");
                        System.out.print("客");
                        System.out.print("\r\n");
                    }
                }
            }

17_多线程(同步方法)(掌握)

        class Printer {
    ​       public static void print1() {
    ​           synchronized(Printer.class){                //锁对象可以是任意对象,但是被锁的代码需要保证是同一把锁,不能用匿名对象
    ​               System.out.print("黑");
    ​               System.out.print("马");
    ​               System.out.print("程");
    ​               System.out.print("序");
    ​               System.out.print("员");
    ​               System.out.print("\r\n");
    ​           }
    ​       }
    ​       /*
    ​        * 非静态同步函数的锁是:this
    ​        * 静态的同步函数的锁是:字节码对象
    ​        */
    ​       public static synchronized void print2() {  
    ​           System.out.print("传");
    ​           System.out.print("智");
    ​           System.out.print("播");
    ​           System.out.print("客");
    ​           System.out.print("\r\n");
    ​       }
    ​   }

18_多线程(线程安全问题)(掌握)

            public class Demo2_Synchronized {

                /**
                 * @param args
                 * 需求:铁路售票,一共100张,通过四个窗口卖完.
                 */
                public static void main(String[] args) {
                    TicketsSeller t1 = new TicketsSeller();
                    TicketsSeller t2 = new TicketsSeller();
                    TicketsSeller t3 = new TicketsSeller();
                    TicketsSeller t4 = new TicketsSeller();
                    
                    t1.setName("窗口1");
                    t2.setName("窗口2");
                    t3.setName("窗口3");
                    t4.setName("窗口4");
                    t1.start();
                    t2.start();
                    t3.start();
                    t4.start();
                }
            
            }
            
            class TicketsSeller extends Thread {
                private static int tickets = 100;
                static Object obj = new Object();
                public TicketsSeller() {
                    super();
                    
                }
                public TicketsSeller(String name) {
                    super(name);
                }
                public void run() {
                    while(true) {
                        synchronized(obj) {
                            if(tickets <= 0) 
                                break;
                            try {
                                Thread.sleep(10);//线程1睡,线程2睡,线程3睡,线程4睡
                            } catch (InterruptedException e) {
                                
                                e.printStackTrace();
                            }
                            System.out.println(getName() + "...这是第" + tickets-- + "号票");
                        }
                    }
                }
            }

19_多线程(火车站卖票的例子用实现Runnable接口)(掌握)

20_多线程(死锁)(了解)

            private static String s1 = "筷子左";
        ​   private static String s2 = "筷子右";
        ​   public static void main(String[] args) {
        ​       new Thread() {
        ​           public void run() {
        ​               while(true) {
        ​                   synchronized(s1) {
        ​                       System.out.println(getName() + "...拿到" + s1 + "等待" + s2);
        ​                       synchronized(s2) {
        ​                           System.out.println(getName() + "...拿到" + s2 + "开吃");
        ​                       }
        ​                   }
        ​               }
        ​           }
        ​       }.start();
        ​       
                new Thread() {
                    public void run() {
                        while(true) {
                            synchronized(s2) {
                                System.out.println(getName() + "...拿到" + s2 + "等待" + s1);
                                synchronized(s1) {
                                    System.out.println(getName() + "...拿到" + s1 + "开吃");
                                }
                            }
                        }
                    }
                }.start();
            }

21_多线程(以前的线程安全的类回顾)(掌握)

上一篇 下一篇

猜你喜欢

热点阅读