zoukankan      html  css  js  c++  java
  • 关于多线程的创建

    import com.sun.deploy.ref.Helpers;

    public class DuoXianCeng {
    public static void main(String[] args) {
    System.out.println("The main method was sdf:"+Thread.currentThread().getName());
    Helper helper = new Helper("Java Thread Anywhere");


    //创建一个线程
    Thread thread = new Thread(helper);
    //设置线程名字
    thread.setName("A-Worker-Thread");
    //启动线程
    thread.start();
    }

    static class Helper implements Runnable {
    private final String message;

    public Helper(String message) {
    this.message = message;
    }

    private void doSomething(String message) {
    System.out.println("The doSomething method ws executed by thread:" + Thread.currentThread().getName());
    System.out.println("Do something with" + message);
    }

    @Override
    public void run() {
    doSomething(message);
    }

    }
    }

    这是创建多线的流程的流程


    * Thread用法

    1、先继承Thread类,重写run()方法,将要运行的代码写到run()方法里;

    2、实例化线程类,调用start()方法,开始线程;

    3、start()方法被调用,执行的是线程类中重写后的run()方法内的代码

    * Runnable用法

    Thread t = new Thread(new RunnableDemo,"ThreadName");

    t.start();




  • 相关阅读:
    匿名方法
    优化从 App.config 读取配置文件
    显示(explicit )与隐式(implicit)转换操作符
    ( 转 ) 聊一聊C#的Equals()和GetHashCode()方法
    协变和逆变
    html frameset的介绍
    html <frame>标签使用
    html <table>标签信息
    html 列表相关信息
    html <form>相关表单
  • 原文地址:https://www.cnblogs.com/LiTu233/p/10812854.html
Copyright © 2011-2022 走看看