public static void main(String[] args) { Runnable r1 = new MyRunnable1(); Runnable r2 = new MyRunnable2(); Thread t1 = new Thread(r1); Thread t2 = new Thread(r2); t1.start(); t2.start(); } } class MyRunnable1 implements Runnable{ public void run(){ for (int i = 0; i < 1000; i++) { System.out.println("你好"); } } } class MyRunnable2 implements Runnable{ public void run(){ for (int i = 0; i < 1000; i++) { System.out.println("再见"); } } }
时间: 2024-01-02 07:02:15 浏览: 53
这是一个Java程序,它创建了两个线程并启动它们,每个线程分别执行一个Runnable对象的run()方法。具体来说,MyRunnable1类和MyRunnable2类都实现了Runnable接口,并覆盖了run()方法。在main()方法中,它们分别被用来创建t1和t2两个线程。当t1和t2线程分别启动后,它们会交替地输出"你好"和"再见",因为它们的任务是分别循环输出这两个字符串。注意,由于线程是异步执行的,因此输出的结果可能不是完全按照顺序交替出现的。
相关问题
class DelayThread exends Thread { private static int count=0; private int no; private int delay; public DelayThread(){ count++; no=count; } public void run(){ try{ for (int i=0;i<10;i++){ delay=(int)(Math.random()*5000); sleep(delay); System.out.println(“线程”+no+“ 的延迟时间是 ”+delay); } }catch(InterruptedException e){} } } public class MyThread{ public static void main(String args[]){ DelayThread thread1=new DelayThread(); DelayThread thread2=new DelayThread(); thread1.start(); thread2.start(); try{ Thread.sleep(1000);} catch(InterruptedException e){ System.out.println(“线程出错!”); } } } 将上列程序利用Runnable接口改写,并上机检验。
下面是利用Runnable接口改写后的程序:
```java
class DelayRunnable implements Runnable {
private static int count = 0;
private int no;
private int delay;
public DelayRunnable(){
count++;
no = count;
}
public void run(){
try{
for (int i = 0; i < 10; i++){
delay = (int)(Math.random()*5000);
Thread.sleep(delay);
System.out.println("线程" + no + "的延迟时间是" + delay);
}
} catch(InterruptedException e){}
}
}
public class MyThread {
public static void main(String[] args){
DelayRunnable r1 = new DelayRunnable();
DelayRunnable r2 = new DelayRunnable();
Thread thread1 = new Thread(r1);
Thread thread2 = new Thread(r2);
thread1.start();
thread2.start();
try{
Thread.sleep(1000);
} catch(InterruptedException e){
System.out.println("线程出错!");
}
}
}
```
程序会创建两个DelayRunnable对象并各自创建线程,并且每个线程都会执行10次循环,每次循环中会随机生成一个延迟时间并让线程休眠这个时间。在main函数中,程序会让主线程休眠1秒钟,以便让其他两个线程有足够的时间执行完毕。
写一个java程序实现Runnable接口,用Thread方法启动线程的方式实现2个线程同时循环输出“线程名-数字”,其中数字从1变化到10.
下面是实现Runnable接口,用Thread方法启动线程的方式实现2个线程同时循环输出“线程名-数字”的Java程序:
```java
public class MyRunnable implements Runnable {
private String name;
private int count;
public MyRunnable(String name) {
this.name = name;
this.count = 1;
}
@Override
public void run() {
while (count <= 10) {
System.out.println(name + "-" + count);
count++;
}
}
}
public class Main {
public static void main(String[] args) {
MyRunnable r1 = new MyRunnable("Thread1");
MyRunnable r2 = new MyRunnable("Thread2");
Thread t1 = new Thread(r1);
Thread t2 = new Thread(r2);
t1.start();
t2.start();
}
}
```
在这个程序中,我们定义了一个MyRunnable类,实现了Runnable接口,并重写了run()方法。该方法将在线程启动时被调用,并循环输出“线程名-数字”,其中数字从1到10。然后,我们在Main类中创建了两个MyRunnable对象,分别给它们赋予“Thread1”和“Thread2”作为线程名。接下来,我们创建了两个Thread对象,并将它们与MyRunnable对象关联起来。最后,我们分别启动了这两个线程,它们会同时执行并输出“线程名-数字”。
阅读全文