多线程实例代码

来源:互联网 发布:怎样手机做淘宝客赚钱 编辑:程序博客网 时间:2024/05/18 22:45
class MyThread implements Runnable{// 实现Runnable接口public void run(){// 覆写run()方法for(int i=0;i<3;i++){System.out.println(Thread.currentThread().getName()+ "运行,i = " + i) ;// 取得当前线程的名字}}};public class CurrentThreadDemo{public static void main(String args[]){MyThread mt = new MyThread() ;// 实例化Runnable子类对象new Thread(mt,"线程").start() ;// 启动线程mt.run() ;// 直接调用run()方法}};

输出结果1:

main运行,i = 0
main运行,i = 1
main运行,i = 2
线程运行,i = 0
线程运行,i = 1
线程运行,i = 2

输出结果2:

main运行,i = 0
线程运行,i = 0
线程运行,i = 1
线程运行,i = 2
main运行,i = 1
main运行,i = 2

0 0
原创粉丝点击