• 两个线程轮流查数


    军训时最常见的莫过于报数了,1、2、3、4、5.....

    现在我要用Java的多线程实现类似军训报数的功能,

    即开启两个线程,让它们轮流数数,从1数到10,如:

    线程A:1

    线程B:2

    线程A:3

    线程B:4

    线程A:5

    线程B:6

    ......

    如何实现该功能呢?

    --------------------------------------------------------------------------------------------------------------------------------

    我的思路:

    如下图所示,我们可以创建两个线程,这两个线程同时调用某个类中的数数方法即可。

    为了维护方便,我们可以将数数方法封装在某个类中,具体如后面的代码所示。

    -------------------------------------------------------------------------------------------------------------------------------------------------------------

    我实现以上功能的程序代码如下:

    [java] view plain copy
     
     print?在CODE上查看代码片派生到我的代码片
    1. import mythread.MyThread;  
    2.   
    3. public class main {  
    4.   
    5.     /** 
    6.      * @param args 
    7.      */  
    8.     public static void main(String[] args) {  
    9.           
    10.         //创建两个线程进行数数  
    11.         MyThread threadA=new MyThread();  
    12.         MyThread threadB=new MyThread();  
    13.         threadA.start();  
    14.         threadB.start();  
    15.     }  
    16.   
    17. }  


     

    [java] view plain copy
     
     print?在CODE上查看代码片派生到我的代码片
    1. package mythread;  
    2. import java.lang.Thread;  
    3.   
    4. //线程类  
    5. public class MyThread extends Thread{  
    6.   
    7.     //注意bs对象必须声明成static  
    8.     //这样在创建两个线程时,它们的bs对象才是同一个对象  
    9.     static Business bs=new Business();  
    10.       
    11.     public void run()  
    12.     {  
    13.         while(bs.getN()<=10)  
    14.         {  
    15.             try {  
    16.                     bs.ShuShu();//开始数数  
    17.             }   
    18.             catch (Exception e) {  
    19.                   
    20.                 e.printStackTrace();  
    21.             }  
    22.         }  
    23.     }  
    24.       
    25. }  
    26.   
    27. //业务类  
    28. class Business{  
    29.     int n=1;  
    30.       
    31.     //数数  
    32.     public synchronized void ShuShu()  
    33.     {  
    34.           
    35.         try {  
    36.             System.out.println("线程:"+Thread.currentThread().getName()+"说:"+n);  
    37.             n++;  
    38.             this.notify();//唤醒处于等待状态的线程  
    39.             this.wait();  //进入等待状态  
    40.         }   
    41.         catch (Exception e) {e.printStackTrace();}  
    42.     }  
    43.       
    44.     public int getN()  
    45.     {  
    46.         return n;  
    47.     }  
    48. }  


    --------------------------------------------------------------------------------------------------------------------------------------------------

    效果截图:

  • 相关阅读:
    HDU 1698 Just a Hook (线段树模板题-区间求和)
    spring定时任务详解(@Scheduled注解)
    spring定时任务(@Scheduled注解)
    java反射实现接口重试
    微信开发者工具在线调试
    消息队列应用场景
    Redis、Memcache和MongoDB的区别
    下拉框多选实现回显及sql
    MySQL 中 You can't specify target table '表名' for update in FROM clause错误解决办法
    catch异常
  • 原文地址:https://www.cnblogs.com/lnas01/p/5947963.html
Copyright © 2020-2023  润新知