在java中多线程并不会继承Thread,因为java中只有单继承,如果继承了Thread类就不可以继承其他类了,所以多用实现Runnable来实现,这样不仅可以继承还可以实现其他类。
public class MyRunnable implements Runnable{
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + "------->" + i);
}
}
}
但是Runable中没有start方法,不可以开一个线程,所以我们还是要用Thread来开线程。
我们可以通过Thread.sleep()的方法来使一个线程进入阻塞状态,可以让其他的线程先运行
public class MyRunnable implements Runnable{
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + "------->" + i);
}
}
}
这样就可以让main线程先运行一秒,然后再并发运行,运行结果如下
main------->0
main------->1
main------->2
main------->3
main------->4
main------->5
main------->6
main------->7
main------->8
main------->9
MyRunnable------->0
MyRunnable------->1
MyRunnable------->2
MyRunnable------->3
MyRunnable------->4
MyRunnable------->5
MyRunnable------->6
MyRunnable------->7
MyRunnable------->8
MyRunnable------->9
Process finished with exit code 0
如果想要在某时刻让他停止睡眠可以用interrupt()方法来停止
public class MyRunnable implements Runnable{
@Override
public void run() {
try {
Thread.sleep(1000*5);
} catch (InterruptedException e) {
e.getStackTrace();
}
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + "------->" + i);
}
}
}
public class Main {
public static void main(String[] args) {
MyRunnable rb = new MyRunnable();
Thread t = new Thread(rb,"MyRunnable");
t.start();
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println(Thread.currentThread().getName() + "------->" + i);
if(i == 3){
t.interrupt();
}
}
}
}
现在的运行结果
main------->0
main------->1
main------->2
main------->3
MyRunnable------->0
MyRunnable------->1
MyRunnable------->2
MyRunnable------->3
MyRunnable------->4
MyRunnable------->5
MyRunnable------->6
MyRunnable------->7
MyRunnable------->8
MyRunnable------->9
main------->4
main------->5
main------->6
main------->7
main------->8
main------->9
Process finished with exit code 0
MyRunnable本来要睡眠5秒的,但是当main线程中i等于3是打断了它的睡眠使它开始运行,由于main每次要睡眠一秒,再这一秒中MyRunnable运行完了。
还有打标志的方法
public class MyRunnable implements Runnable{
boolean b = true;
@Override
public void run() {
for (int i = 0; i < 10; i++) {
if(b) {
try {
Thread.sleep(1000 * 5);
} catch (InterruptedException e) {
e.getStackTrace();
}
}
System.out.println(Thread.currentThread().getName() + "------->" + i);
}
}
}
public class Main {
public static void main(String[] args) {
MyRunnable rb = new MyRunnable();
Thread t = new Thread(rb,"MyRunnable");
t.start();
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println(Thread.currentThread().getName() + "------->" + i);
if(i == 3){
rb.b=false;
}
}
}
}
运行结果
main------->0
main------->1
main------->2
main------->3
MyRunnable------->0
MyRunnable------->1
MyRunnable------->2
MyRunnable------->3
MyRunnable------->4
MyRunnable------->5
MyRunnable------->6
MyRunnable------->7
MyRunnable------->8
MyRunnable------->9
main------->4
main------->5
main------->6
main------->7
main------->8
main------->9
Process finished with exit code 0
当main线程到3的时候b等于false,MyRunnable不会进入睡眠。