创建实现Runnable接口的线程
class MyDemo implements Runnable{
@Override
public void run() {
for (int i = 1; i <= 50; i++) {
System.out.println(Thread.currentThread().getName()+"*****run*******" + i);
}
}
}
public class Test03 {
public static void main(String[] args) {
MyDemo d=new MyDemo();
Thread t=new Thread(d,"MyThread");
t.start();
Thread thread=Thread.currentThread();
for (int i = 1; i <= 50; i++) {
System.out.println(thread.getName()+"*****Hello World*******" + i);
}
}
}
多线程
public class Test04 {
public static void main(String[] args) {
MyThread2 mt=new MyThread2();
Thread t1=new Thread(mt, "窗口1");
Thread t2=new Thread(mt, "窗口2");
t1.start();
t2.start();
}
}
class MyThread1 extends Thread {
static int num = 50;
@Override
public void run() {
while (num > 0) {
System.out.println(Thread.currentThread().getName() + "******"
+ num--);
}
}
}
class MyThread2 implements Runnable {
int num = 5000;
@Override
public void run() {
while (num > 0) {
System.out.println(Thread.currentThread().getName() + "******"
+ num--);
}
}
}