class Demo implements Runnable{
private int ticket=10;
public void run(){
while(ticket>0){
System.out.println(this+"开始卖票,剩余张数:"+ticket);
ticket--;
}
}
}
public class ThreadDemo01{
public static void main(String args[]){
Demo d=new Demo();
Thread t1=new Thread(d);
Thread t2=new Thread(d);
Thread t3=new Thread(d);
t1.setName("1号售票口");
t2.setName("2号售票口");
t2.setName("3号售票口");
t1.start();
t2.start();
t3.start();
}
}
package duotai;
class Demo implements Runnable{
private int ticket=10;
public void run(){
while(ticket>0){
System.out.println(Thread.currentThread().getName()+"开始卖票,剩余张数:"+ticket--);
}
}
}
public class ThreadDemo01{
public static void main(String args[]){
Demo d=new Demo();
Thread t1=new Thread(d);
Thread t2=new Thread(d);
Thread t3=new Thread(d);
t1.setName("1号售票口");
t2.setName("2号售票口");
t2.setName("3号售票口");
t1.start();
t2.start();
t3.start();
}
}
运行结果很奇葩~
Thread对象命名,this处怎么打印当前线程命名
答案:1 悬赏:0 手机版
解决时间 2021-03-09 23:10
- 提问者网友:趣果有间
- 2021-03-08 23:05
最佳答案
- 五星知识达人网友:几近狂妄
- 2021-03-09 00:17
你要的是这样的效果?
class Demo implements Runnable {
private int ticket = 10;
public void run() {
while (ticket > 0) {
System.out.println(Thread.currentThread().getName() + "开始卖票,剩余张数:" + ticket);
ticket--;
}
}
}
public class ThreadDemo01 {
public static void main(String args[]) {
Demo d = new Demo();
Thread t1 = new Thread(d);
Thread t2 = new Thread(d);
Thread t3 = new Thread(d);
t1.setName("1号售票口");
t2.setName("2号售票口");
t3.setName("3号售票口");
t1.start();
t2.start();
t3.start();
}
}
class Demo implements Runnable {
private int ticket = 10;
public void run() {
while (ticket > 0) {
System.out.println(Thread.currentThread().getName() + "开始卖票,剩余张数:" + ticket);
ticket--;
}
}
}
public class ThreadDemo01 {
public static void main(String args[]) {
Demo d = new Demo();
Thread t1 = new Thread(d);
Thread t2 = new Thread(d);
Thread t3 = new Thread(d);
t1.setName("1号售票口");
t2.setName("2号售票口");
t3.setName("3号售票口");
t1.start();
t2.start();
t3.start();
}
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯