线程面试题目

  1. Java代码
    1. package com.study.thread;  
    2.   
    3. public class MyThread  
    4. {  
    5.     private static Count count = new Count(1);  
    6.   
    7.     private static SubThread s = new SubThread(count);  
    8.   
    9.     /** 
    10.      * 子线程循环10次,接着主线程循环100,接着又回到子线程循环10次, 
    11.       接着再回到主线程又循环100,如此循环50次,请写出程序。  
    12.      */  
    13.     public static void main(String[] args) throws Exception  
    14.     {  
    15.         Thread t = new Thread(s, "子线程");  
    16.         t.start();  
    17.         Thread.sleep(1);  
    18.         mainFun();  
    19.     }  
    20.   
    21.   
    22.     private static void mainFun()  
    23.     {  
    24.         synchronized (count)  
    25.         {  
    26.             while (count.count <= 50)  
    27.             {  
    28.                 for (int i = 0; i < 100; i++)  
    29.                 {  
    30.                     System.out.println(Thread.currentThread().getName() + "执行第" + (i + 1) + "次");  
    31.                 }  
    32.                 try  
    33.                 {  
    34.                     count.notify();  
    35.                     count.wait();  
    36.                 }  
    37.                 catch (InterruptedException e)  
    38.                 {  
    39.                     // TODO Auto-generated catch block  
    40.                     e.printStackTrace();  
    41.                 }  
    42.   
    43.             }  
    44.         }  
    45.     }  
    46.   
    47.     static class SubThread implements Runnable  
    48.     {  
    49.         Count count;  
    50.   
    51.   
    52.         public SubThread(Count count)  
    53.         {  
    54.             this.count = count;  
    55.         }  
    56.   
    57.   
    58.         public void run()  
    59.         {  
    60.             synchronized (count)  
    61.             {  
    62.                 while (count.count <= 50)  
    63.                 {  
    64.                     System.out.println("第" + count.count + "次循环----------------------");  
    65.                     count.count++;  
    66.                     for (int i = 0; i < 10; i++)  
    67.                     {  
    68.                         System.out.println(Thread.currentThread().getName() + "执行第" + (i + 1) + "次");  
    69.                     }  
    70.                     try  
    71.                     {  
    72.                         count.notify();  
    73.                         count.wait();  
    74.                     }  
    75.                     catch (InterruptedException e)  
    76.                     {  
    77.                         e.printStackTrace();  
    78.                     }  
    79.                 }  
    80.             }  
    81.         }  
    82.     }  
    83.   
    84.     static class Count  
    85.     {  
    86.         int count;  
    87.   
    88.   
    89.         public Count(int count)  
    90.         {  
    91.             this.count = count;  
    92.         }  
    93.   
    94.   
    95.         public int getCount()  
    96.         {  
    97.             return count;  
    98.         }  
    99.   
    100.     }  
    101.   
    102. }  

     

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值