zoukankan      html  css  js  c++  java
  • 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;
                }
            }
        }
    }

  • 相关阅读:
    SpringBoot集成Swagger2中不同环境开关配置
    mysql
    pip 命令汇总
    mysql 时间查询(当天、本周,本月,上一个月的数据)
    MYSQL 常用函数
    java8 array、list操作 汇【6】)- Java8 Lambda表达式增强版Comparator和排序
    解决pip安装超时的问题
    Java对象为空时,将null转换为"" 保存值为空的属性
    mysql -- 模糊查询的四种方法
    Mysql 查询以某个字符开头的语句
  • 原文地址:https://www.cnblogs.com/MR-Guo/p/3372988.html
Copyright © 2011-2022 走看看