zoukankan      html  css  js  c++  java
  • java 线程实现方式

    java实现多线程的方式有三种:

    1.继承Thread类 ,重写run()方法,用start()方法启动线程。

    public class threadDemo extends Thread(){
    public void run{};
    public static void main(String[] args) {
    new threadDemo.start();
    }
    }
    

    2.实现runnable接口,重写run()方法。

    1 public class Runable implement runnable(){
    2 public void run(){
    3 }
    4 public static void main(String[] args) {
    5 new Thread(new Runable).start();
    6 }
    7 }

    3.实现callable接口,与runnable类似,但是callable可有返回值也可抛出异常。

    4.继承Timertask类,重写run(),使用Timer启动定时任务。

    public class ClockTask extends TimerTask{
    
        @Override
        public void run() {
                System.out.println("任务开始了!");    
        }
            public static void main(String[] args) {
            Timer time=new Timer();
            ClockTask ct=new ClockTask();
            time.schedule(ct,2000,2000);
        }
    }
  • 相关阅读:
    runtime iOS 运行时机制
    iOS 文件操作
    responseCode 状态吗查询
    iOS常用宏定义
    Block里用self造成循环引用
    iOS Block全面分析
    OC与Swift混编
    iOS打包app发给测试人员测试
    Swift UITextField
    sqilite学习
  • 原文地址:https://www.cnblogs.com/xiao-v/p/4547874.html
Copyright © 2011-2022 走看看