package com.bb;
/**
* @Auther: bobo
* @Date: 2018/11/17 09:56
* @Description:
*/
public class Demo1 {
public static void main(String[] args) {
Runnable1 run = new Runnable1();
Runnable2 run2 = new Runnable2();
Thread thread = new Thread(run);
Thread thread2 = new Thread(run2);
thread.start();
thread2.start();
}
}
class Runnable1 implements Runnable {
@Override
public void run() {
for (int i = 1; i < 5000; i++) {
System.err.println("当前数为 :" + i);
if ((i % 5) == 0) {
System.err.println("*****************当前行是5 的倍数 , i 的值是:" + i +
"****************" + "RUNNABLE 1");
try {
Thread.sleep(200);
System.err.println(" 当前行是5 的倍数 线程等待2000ms");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
System.err.println("o(∩_∩)o 哈哈 ,线程 1 结束了");
}
}
class Runnable2 implements Runnable {
@Override
public void run() {
for (int i = 1; i < 5000; i++) {
System.err.println("当前数为 :" + i + "RUNNABLE 2");
if ((i % 2) == 0) {
System.err.println("*****************当前行是2 的倍数 , i 的值是:" + i +
"****************" + "RUNNABLE 2");
try {
Thread.sleep(100);
System.err.println(" 当前行是2 的倍数 , 线程等待1000ms");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
System.err.println("o(∩_∩)o 哈哈 o(∩_∩)o 哈哈 ,线程 2 结束了");
}
}
最简单的多线程
最新推荐文章于 2021-10-19 14:49:22 发布