线程基础之产生线程

来源:互联网 发布:程序员必备生活用品 编辑:程序博客网 时间:2024/06/06 02:40

产生线程:继承Thread和实现Runnable


直接上代码微笑


package com.lyj;public class TraditionalThreadDemo1 {    public static void main(String[] args) {        Thread thread1 = new Thread() {            @Override            public void run() {                super.run();                while (true) {                    try {                        Thread.sleep(500);                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                    System.out.println("Thread1: " + Thread.currentThread().getName());                }            }        };        thread1.start();        Thread thread2 = new Thread(new Runnable() {            @Override            public void run() {                while (true) {                    try {                        Thread.sleep(500);                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                    System.out.println("Thread2: " + Thread.currentThread().getName());                }            }        });        thread2.start();    }}


原创粉丝点击