zoukankan      html  css  js  c++  java
  • Java多线程基础

    1.通过继承Thread类实现多线程:

    public class Thread1 extends Thread{
        /**
         * 该线程类的线程体
         */
        public void run(){ //线程体
            for (int i = 0; i < 100; i++) {
                System.out.println(this.getName()+":"+i);
            }
        }
        
        public static void main(String[] args) {
            Thread1 a = new Thread1();
            a.start();
            Thread1 b = new Thread1();
            b.start();
        }
    }

    2.通过实现Runnable接口实现多线程:

    public class Thread2 implements Runnable{
        int threadId;
        
        public Thread2(int id) {
            super();
            this.threadId = id;
        }
    
        @Override
        public void run() {
            for (int i = 0; i < 100; i++) {
                System.out.println(this.threadId+":"+i);
            }
        }
        
        public static void main(String[] args) {
            Thread a = new Thread(new Thread2(1));
            a.start();
            Thread b = new Thread(new Thread2(2));
            b.start();
        }
    }
  • 相关阅读:
    反射
    IO流
    集合(下)
    集合(上)
    泛型
    异常
    常用类
    内部类
    将博客搬至CSDN
    DBMS_ERRLOG记录DML错误日志(二)
  • 原文地址:https://www.cnblogs.com/lxcmyf/p/5716382.html
Copyright © 2011-2022 走看看