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);
        }
    }
  • 相关阅读:
    python bif 如何自学
    python萌新应知应会
    Animation
    响应式布局
    浏览器兼容
    HTML基础
    SublimeText 3 Emmet Hot Keys
    Web大前端环境搭建
    Sublime Text 运行js
    bash脚本编程基础
  • 原文地址:https://www.cnblogs.com/xiao-v/p/4547874.html
Copyright © 2011-2022 走看看