62,614
社区成员
发帖
与我相关
我的任务
分享
public class BB extends Thread {
int time;
public BB (int t) {
this.time = t;
}
public synchronized void run() {
for(int i = 1; i <= 5; i++) {
System.out.println(Thread.currentThread().getName() + " " + i + "次");
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(i ==5) {
System.out.println(Thread.currentThread().getName() + "退出了");
}
}
}
public static void main(String[] args) {
BB b1 = new BB(500);
b1.setName("线程1");
b1.start();
BB b2 = new BB(200);
b2.setName("线程2");
b2.start();
BB b3 = new BB(300);
b3.setName("线程3");
b3.start();
}
}
@Override
public synchronized void run() {
for (int i = 1; i <= 5; i++) {
System.out.println(Thread.currentThread().getName() + " " + i + "次");
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (i == 5) {
System.out.println(Thread.currentThread().getName() + "退出了");
time -= 200;
}
}
}
package bb;
public class BB implements Runnable {
int time;
public BB(int t) {
time = t;
}
@Override
public synchronized void run() {
for (int i = 1; i <= 5; i++) {
System.out.println(Thread.currentThread().getName() + " " + i + "次");
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (i == 5) {
System.out.println(Thread.currentThread().getName() + "退出了");
}
}
}
public static void main(String[] args) {
BB b = new BB(500);
new Thread(b).start();
new Thread(b).start();
new Thread(b).start();
}
}
public class BB extends Thread {
int time;
public BB (int t) {
this.time = t;
}
public void run() {
synchronized (BB.class)
for(int i = 1; i <= 5; i++) {
System.out.println(Thread.currentThread().getName() + " " + i + "次");
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(i ==5) {
System.out.println(Thread.currentThread().getName() + "退出了");
}
}
}
}
public static void main(String[] args) {
BB b1 = new BB(500);
b1.setName("线程1");
b1.start();
BB b2 = new BB(200);
b2.setName("线程2");
b2.start();
BB b3 = new BB(300);
b3.setName("线程3");
b3.start();
}
}
System.out.println("This object is "+this);