• multithreadingDemo


    class Count implements Runnable
    {
       Thread mythread ;
       Count()
       { 
          mythread = new Thread(this, "my runnable thread");
          System.out.println("my thread created" + mythread);
          mythread.start();
       }
       public void run()
       {
          try
          {
            for (int i=0 ;i<10;i++)
            {
              System.out.println("Printing the count " + i);
              Thread.sleep(1000);
            }
         }
         catch(InterruptedException e)
         {
            System.out.println("my thread interrupted");
         }
         System.out.println("mythread run is over" );
       }
    }
    

      

    class RunnableExample
    {
        public static void main(String args[])
        {
           Count cnt = new Count();
           try
           {
              while(cnt.mythread.isAlive())
              {
                System.out.println("Main thread will be alive till the child thread is live"); 
                Thread.sleep(1500);
              }
           }
           catch(InterruptedException e)
           {
              System.out.println("Main thread interrupted");
           }
           System.out.println("Main thread run is over" );
        }
    }
    

      

    my thread createdThread[my runnable thread,5,main]
    Main thread will be alive till the child thread is live
    Printing the count 0
    Printing the count 1
    Main thread will be alive till the child thread is live
    Printing the count 2
    Main thread will be alive till the child thread is live
    Printing the count 3
    Printing the count 4
    Main thread will be alive till the child thread is live
    Printing the count 5
    Main thread will be alive till the child thread is live
    Printing the count 6
    Printing the count 7
    Main thread will be alive till the child thread is live
    Printing the count 8
    Main thread will be alive till the child thread is live
    Printing the count 9
    mythread run is over
    Main thread run is over


  • 相关阅读:
    十大排序算法之选择排序(2)
    十大排序算法之冒泡排序(1)
    2018年年度总结
    敏捷软件开发学习笔记(四)之结构型设计模式
    敏捷软件开发学习笔记(三)之创造型设计模式
    elasticsearch+head+kibana
    闭包函数延迟绑定问题
    初谈dango的post提交csrf设置和文件上传
    浏览器的同源策略,及如可跨域
    socket并发通信的几种方式
  • 原文地址:https://www.cnblogs.com/lnas01/p/4528158.html
Copyright © 2020-2023  润新知