zoukankan      html  css  js  c++  java
  • 1.线程--线程创建方式

    线程与进程的区别:进程是所有线程的集合,每一个线程是进程中的一条执行路径
     
    多线程的好处:提高程序效率
     
    创建线程的两种方法:1.继承Thread类重写 run()方法 ,2.实现Runnable接口重写run()方法,匿名内部类
    第二种方法比较好,继承只能单继承,而实现可以多实现
     
    1.继承Thread
    class CreateThread extends Thread{
        @Override
        public void run() {
            for (int i = 0; i <100 ; i++) {
                System.out.println("run()"+i);
    //getId(); 获取线程id getName();获取线程名字
            }
        }
    }
    CreateThread createThread = new CreateThread();
    createThread.start();
    2.实现Runnable
    class CreateRunnable implements Runnable{
     
        public void run() {
            for (int i = 0; i <100 ; i++) {
                System.out.println("run()"+i);
    //ThreadCurrentThread().getId() 获取线程id
            }
        }
    }
    CreateRunnable createRunnable = new CreateRunnable();
    Thread thread = new Thread(createRunnable);
    thread.start();
     
    3.匿名内部类
    new Thread(new Runnable() {
         public void run() {
             for (int i = 0; i < 100; i++) {
                 System.out.println("run:" + i);
             }
         }
    }).start();
  • 相关阅读:
    python返回函数与匿名函数
    Session&Cookie
    write RE validation
    hello2 source anaylis
    Filter
    Development descriptor
    web.xml配置详解
    Annotation
    injector
    container
  • 原文地址:https://www.cnblogs.com/goldlong/p/10953822.html
Copyright © 2011-2022 走看看