zoukankan      html  css  js  c++  java
  • Java多线程

      1. 一:实现多线程的两种方式:
      2.  
         
      3.  
        1:从Thread类继承,并重写run方法。
      4.  
         
      5.  
        2:实现Runnable接口,并实现其中的run方法。
      6.  
         
      7.  
        二:Java语言对多线的一些需要注意的事项。
      8.  
         
      9.  
        1:Java运行时系统实现了一个用于调度线程执行的线程调度器,用于确定某一时刻由哪一个线程在CPU上运行。
      10.  
         
      11.  
        2:在java技术中,线程通常是抢占式的而不需要时间片分配进程(分配给每个线程相等的CPU时间的进程)。抢占式调度模型就是许多线程处于可以运行状态(等待状态),但实际上只有一个线程在运行。该线程一直运行到它终止进入可运行状态(等待状态),或者另一个具有更高优先级的线程变成可运行状态。在后一种情况下,低优先级的线程被高优先级的线程抢占,高优先级的线程获得运行的机会。
      12.  
         
      13.  
        3:Java线程调度器支持不同优先级线程的抢先方式,但其本身不支持相同优先级线程的时间片轮换。
      14.  
         
      15.  
        4:Java运行时系统所在的操作系统(例如:Windows2000)支持时间片的轮换,则线程调度器就支持相同优先级线程的时间片轮换。
      16.  
         
      17.  
        5:不可以过度依赖系统的线程调度器,例如:拥有低优先级的线程也有可能获取到执行的机会。
      18.  
         
      19.  
        三:示例:
      20.  
         
      21.  
        Thread 类:
      22.  
         
      23.  
        class TestThread
      24.  
        {
      25.  
        public static void main(String[] args)
      26.  
        {
      27.  
        MyThread mt=new MyThread();
      28.  
        mt.start();
      29.  
         
      30.  
        }
      31.  
        }
      32.  
        class MyThread extends Thread
      33.  
        {
      34.  
        public void run()
      35.  
        {
      36.  
        while(true)
      37.  
        {
      38.  
        System.out.println("MyThread is running ");
      39.  
        }
      40.  
        }
      41.  
        }
      42.  
         
      43.  
         
      44.  
        代码很容易理解,派生类重写了run方法。当然Thread还有许多其他的方法可以重写,这个也是我们派生Thread的一个优势的地方。
      45.  
         
      46.  
        Runnable接口:
      47.  
         
      48.  
        class TestThread
      49.  
        {
      50.  
        public static void main(String[] args)
      51.  
        {
      52.  
        MyRunnable mr=new MyRunnable();
      53.  
        Thread thread=new Thread(mr);
      54.  
        //mt.setDaemon(true);//是否后台线程
      55.  
        //mt.setPriority(Thread.MAX_PRIORITY);//设置优先级
      56.  
        mt.start();
      57.  
        //new Thread(mr).start();
      58.  
         
      59.  
         
      60.  
        }
      61.  
        }
      62.  
        class MyRunnable implements Runnable
      63.  
        {
      64.  
        public void run()
      65.  
        {
      66.  
        while(true)
      67.  
        {
      68.  
        System.out.println("MyRunnable is running ");
      69.  
        }
      70.  
        }
      71.  
        }
      72.  
         
      73.  
         
      74.  
        Thread类派生和Runnable接口实现没有很大的区别,一般我们用Runnable接口的比较多,因为Java不支持多继承,当然前提是你不希望重写有关Thread类的方法。
      75.  
         
      76.  
        四:线程的同步:
      77.  
         
      78.  
        线程的同步的原因就是多个线程访问同一个资源,在资源的访问的过程中出现时间片的切换导致同时多个线程都进入到了资源操作的过程中。也就是说我们需要避免多个线程能同时访问同一个资源。就有像是在商场试衣间试衣服,只能有一个人在一个时间进入试衣间试衣服。多个人拿着衣服,可以理解多个线程在跑。试衣间就是一个大家公共的资源,只能一个人去访问。
      79.  
         
      80.  
        简而言之,Java同步的方式有两种:
      81.  
         
      82.  
        1:同步块
      83.  
         
      84.  
        2:同步方法
      85.  
         
      86.  
        这里需要准备一些一些基础知识:
      87.  
         
      88.  
        1:每个对象都有一个监视器,或者叫做锁。
      89.  
         
      90.  
        2:同步方法是利用this对象的锁。这种一般应用在同步方法。
      91.  
         
      92.  
        3:每个类class也有一个锁,也就是这个class所对应的对象的这个所。这个一般用于类的静态方法。
      93.  
         
      94.  
        class TestThread
      95.  
        {
      96.  
        public static void main(String[] args)
      97.  
        {
      98.  
        MyRunnable mr=new MyRunnable();
      99.  
         
      100.  
        new Thread(mr).start();
      101.  
        new Thread(mr).start();
      102.  
        new Thread(mr).start();
      103.  
        new Thread(mr).start();
      104.  
        new Thread(mr).start();
      105.  
        new Thread(mr).start();
      106.  
         
      107.  
        }
      108.  
        }
      109.  
        class MyRunnable implements Runnable
      110.  
        {
      111.  
        Object o=new Object();
      112.  
        int index=0;
      113.  
         
      114.  
        public void run()
      115.  
        {
      116.  
        while(true)
      117.  
        {
      118.  
        synchronized(o)
      119.  
        {
      120.  
        System.out.println("Thread ID = "+Thread.currentThread().getName()+"Index ="+index++);
      121.  
        }
      122.  
        }
      123.  
        }
      124.  
        }
      125.  
         
      126.  
         
      127.  
        这样就基本保证了对于大家公共的资源Index变量的访问没有问题。
      128.  
         
      129.  
        下面就是利用同步方法的方式,它锁住的对象是this,上面同步块的情况如果把synchronized参数改成this,也就是一样的效果了。
      130.  
         
      131.  
        class TestThread
      132.  
        {
      133.  
        public static void main(String[] args)
      134.  
        {
      135.  
        MyRunnable mr=new MyRunnable();
      136.  
         
      137.  
        new Thread(mr).start();
      138.  
        new Thread(mr).start();
      139.  
        new Thread(mr).start();
      140.  
        new Thread(mr).start();
      141.  
        new Thread(mr).start();
      142.  
        new Thread(mr).start();
      143.  
         
      144.  
        }
      145.  
        }
      146.  
        class MyRunnable implements Runnable
      147.  
        {
      148.  
        Object o=new Object();
      149.  
        int index=0;
      150.  
         
      151.  
        public void run()
      152.  
        {
      153.  
        while(true)
      154.  
        {
      155.  
        /*synchronized(o)
      156.  
        {
      157.  
        System.out.println("Thread ID = "+Thread.currentThread().getName()+"Index ="+index++);
      158.  
        }
      159.  
        */
      160.  
        output();
      161.  
        }
      162.  
        }
      163.  
        public synchronized void output()
      164.  
        {
      165.  
        System.out.println("Thread ID = "+Thread.currentThread().getName()+"Index ="+index++);
      166.  
        }
      167.  
        }
      168.  
         
      169.  
         
      170.  
        五:线程的死锁。
      171.  
         
      172.  
        每一个对象除了有一个锁之外,还有一个等待队列(wait set),当一个对象刚创建的时候,它的对待队列是空的。
      173.  
        我们应该在当前线程锁住对象的锁后,去调用该对象的wait方法。
      174.  
        当调用对象的notify方法时,将从该对象的等待队列中删除一个任意选择的线程,这个线程将再次成为可运行的线程。
      175.  
        当调用对象的notifyAll方法时,将从该对象的等待队列中删除所有等待的线程,这些线程将成为可运行的线程。
      176.  
        wait和notify主要用于producer-consumer这种关系中。
      177.  
         
      178.  
         
      179.  
        class TicketsSystem
      180.  
        {
      181.  
        public static void main(String[] args)
      182.  
        {
      183.  
        SellThread st=new SellThread();
      184.  
         
      185.  
        new Thread(st).start();
      186.  
        try
      187.  
        {
      188.  
        Thread.sleep(1);
      189.  
        }
      190.  
        catch(Exception e)
      191.  
        {
      192.  
        e.printStackTrace();
      193.  
        }
      194.  
        st.b=true;
      195.  
        new Thread(st).start();
      196.  
        //new Thread(st).start();
      197.  
        //new Thread(st).start();
      198.  
        }
      199.  
        }
      200.  
        class SellThread implements Runnable
      201.  
        {
      202.  
        int tickets=100;
      203.  
        Object obj=new Object();
      204.  
        boolean b=false;
      205.  
        public void run()
      206.  
        {
      207.  
        if(b==false)
      208.  
        {
      209.  
        while(true)
      210.  
        sell();
      211.  
        }
      212.  
        else
      213.  
        {
      214.  
        while(true)
      215.  
        {
      216.  
        synchronized(obj)
      217.  
        {
      218.  
        try
      219.  
        {
      220.  
        Thread.sleep(10);
      221.  
        }
      222.  
        catch(Exception e)
      223.  
        {
      224.  
        e.printStackTrace();
      225.  
        }
      226.  
        synchronized(this)
      227.  
        {
      228.  
        if(tickets>0)
      229.  
        {
      230.  
         
      231.  
        System.out.println("obj:"+Thread.currentThread().getName()+
      232.  
        " sell tickets:"+tickets);
      233.  
        tickets--;
      234.  
        }
      235.  
        }
      236.  
        }
      237.  
        }
      238.  
        }
      239.  
        }
      240.  
        public synchronized void sell()
      241.  
        {
      242.  
        synchronized(obj)
      243.  
        {
      244.  
        if(tickets>0)
      245.  
        {
      246.  
        try
      247.  
        {
      248.  
        Thread.sleep(10);
      249.  
        }
      250.  
        catch(Exception e)
      251.  
        {
      252.  
        e.printStackTrace();
      253.  
        }
      254.  
        System.out.println("sell():"+Thread.currentThread().getName()+
      255.  
        " sell tickets:"+tickets);
      256.  
        tickets--;
      257.  
        }
      258.  
        }
      259.  
        }
      260.  
  • 相关阅读:
    两步验证杀手锏:Java 接入 Google 身份验证器实战
    涨姿势:Spring Boot 2.x 启动全过程源码分析
    Spring Cloud 升级最新 Finchley 版本,踩了所有的坑!
    Spring Boot 2.x 启动全过程源码分析(上)入口类剖析
    推荐:7 月份值得一看的 Java 技术干货!
    屌炸天,Oracle 发布了一个全栈虚拟机 GraalVM,支持 Python!
    Spring Boot 核心配置文件 bootstrap & application 详解。
    出场率比较高的一道多线程安全面试题
    凉凉了,Eureka 2.x 停止维护,Spring Cloud 何去何从?
    读写Excel
  • 原文地址:https://www.cnblogs.com/LWK5100/p/14231585.html
Copyright © 2011-2022 走看看