1. 编一个程序,实现两个线程 A 和 B ,A 和 B 同时启动,A线程每隔5秒显示字符串"A
展开全部
package com.test.junit;
import java.util.Date;
public class MyThread extends Thread {
//间隔时间(毫秒)
private int sm;
//输出
private String psm;
//重写线程run方法
public void run() {
try {
while (true) {
if (psm != null) {
System.out.println(Thread.currentThread().getName()+" msg:A ,time:" +new Date());
}
if (sm != 0) {
Thread.sleep(sm);
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//构造函数
public MyThread(int sm, String psm) {
this.sm = sm;
this.psm = psm;
}
public MyThread() {
}
//测试
public static void main(String[] args) {
MyThread a = new MyThread(5000, "A");
MyThread b = new MyThread();
a.start();
b.start();
}
}
import java.util.Date;
public class MyThread extends Thread {
//间隔时间(毫秒)
private int sm;
//输出
private String psm;
//重写线程run方法
public void run() {
try {
while (true) {
if (psm != null) {
System.out.println(Thread.currentThread().getName()+" msg:A ,time:" +new Date());
}
if (sm != 0) {
Thread.sleep(sm);
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//构造函数
public MyThread(int sm, String psm) {
this.sm = sm;
this.psm = psm;
}
public MyThread() {
}
//测试
public static void main(String[] args) {
MyThread a = new MyThread(5000, "A");
MyThread b = new MyThread();
a.start();
b.start();
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询