实现进程的两种方式
方式一:继承Tread类
class Mythread extends Tread{
@override
public void run(){
//程序逻辑部分
}
}
Mythread m = new Mythread();
m.start();
方式二:实现Runable()接口
class MyRunable implements Runable{
@override
public void run(){
//程序逻辑部分
}
}
MyRunable mr = new MyRunable();
Thread t = new Tread(mr);
t.start();
代码案例:
package com.thread; public class TreadDemo1 { public static void main(String[] args) { MyThread mt = new MyThread(); mt.start(); MyRunable mr = new MyRunable(); Thread t = new Thread(mr); t.run(); } } //方式一: 继承Tread类 class MyThread extends Thread{ @Override public void run() { // 程序逻辑部分 for (int i = 0; i < 1000; i++) { System.out.println(Thread.currentThread().getName()+"-"+i); } } } //方式二: 实现Runable接口 class MyRunable implements Runnable{ @Override public void run() { // 程序逻辑部分 for (int i = 0; i < 1000; i++) { System.out.println(Thread.currentThread().getName()+"-"+i); } } }
本文来自博客园,作者:藤原豆腐渣渣,转载请注明原文链接:https://www.cnblogs.com/javafufeng/p/16723417.html