• JAVA--实现线程


    /*
    线程两种方法中的第一种方法
    class XianCheng
    {
        public  static void main(String[] args)
        {
            Cat dog=new Cat();
            dog.start();


        }
    }
    class Cat extends Thread
    {
        public void run()
        {
            int times=0;
            while(true)
              {
                    //休眠一秒,线程一遇到sleep就会进入到blocked状态,并释放资源
                try{
                    Thread.sleep(1000);
                }catch(Exception e){
                    e.printStackTrace();
                }
                times++;
                System.out.println("this is my test "+ times);
                if (times==10)
                {
                    break;
                }
              }
        }
    }
    */

    //线程两种方法中的第二种方法
    public class XianCheng
    {
        public static void main(String[] args)
        {
            Cat dog=new Cat();
            Thread t=new Thread(dog);
            t.start();
        }
    }
    class Cat implements Runnable
    {
        int times=0;
        public void run()
        {
            while(true)
            {
                try
                {
                    Thread.sleep(1000);
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
                times++;
                System.out.println("this is a test "+times);
                if (times==10)
                {
                    break;
                }
            }
        }
    }

  • 相关阅读:
    Atitit.执行cli cmd的原理与调试
    Atitit. IE8.0 显示本地图片预览解决方案 img.src=本地图片路径无效的解决方案
    SlidingDrawer的简单实例(抽屉效果)
    Android android.intent.category解析
    layout中加载gif图片
    Java 中 Vector、ArrayList、List 使用深入剖析
    定时器的简单实例
    supportsscreens
    Android屏幕元素层次结构
    panel的抽屉效果
  • 原文地址:https://www.cnblogs.com/MR-Guo/p/3372988.html
Copyright © 2020-2023  润新知