qq_慕函数8456880
2019-04-23 17:11
package com.xuanxuan;
public class Actor extends Thread {
public void run() {
System.out.println(getName()+"是一个演员");
int count=0;
boolean keepRuning=true;
while(keepRuning) {
System.out.println(getName()+"登台演出"+(++count));
if(count==100) {
keepRuning=false;
}
if(count%10==0) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
System.out.println(getName()+"演出结束了");
}
class Actress implements Runnable{
public void run() {
System.out.println(Thread.currentThread().getName()+"是一个演员");
int count=0;
boolean keepRuning=true;
while(keepRuning) {
System.out.println(Thread.currentThread().getName()+"登台演出"+(++count));
if(count==100) {
keepRuning=false;
}
if(count%10==0) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
System.out.println(Thread.currentThread().getName()+"演出结束了");
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Thread actor=new Actor();
actor.setName("先生");
Thread actressThread=new Thread(new Actress(),"女士");
actor.start();
actressThread.start();
}
}
class Actress implements Runnable{
public void run() {
System.out.println(Thread.currentThread().getName()+"是一个演员");
int count=0;
boolean keepRuning=true;
while(keepRuning) {
System.out.println(Thread.currentThread().getName()+"登台演出"+(++count));
if(count==100) {
keepRuning=false;
}
if(count%10==0) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
System.out.println(Thread.currentThread().getName()+"演出结束了");
}
}
这个class Actress叫做内部类,他和public class Actor是同级的,所以Actress应该放在Actor的大括号的外面。
Actress类放外面。不是放在Actor里面的
在你的Actress类前面加个static
深入浅出Java多线程
186071 学习 · 498 问题
相似问题