现在的位置: 首页 > 综合 > 正文

线程基础之产生线程

2018年05月26日 ⁄ 综合 ⁄ 共 580字 ⁄ 字号 评论关闭

产生线程:继承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();

    }

}

【上篇】
【下篇】

抱歉!评论已关闭.