課程
/后端開發(fā)
/Java
/深入淺出Java多線程
為什么如題的代碼運(yùn)行結(jié)果不對(duì) 編譯器也不報(bào)錯(cuò)呢
2016-10-25
源自:深入淺出Java多線程 2-3
正在回答
package com.imooc.concurrent;
public class Actor extends Thread {
public void run() {
System.out.println(getName() + "is an actor");
int count = 0;
boolean a=true;
while (a){
System.out.println(getName() + "show begins" + (++count));
if (count==100)
{
a=false;
}
if(count%10==0){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
// System.out.println(getName() + "show begins" + (++count));
System.out.println(getName() + "show is over");
public static void main(String[] args) {
// Thread actor = new Actor();
//
// actor.setName("Mr.Thread");
Thread actor=new Thread(new Actor(),"Mr.Thread");
actor.start();
Thread actressThread= new Thread(new Actress(),"Ms.Runnable");
actressThread.start();
?class Actress implements Runnable{
System.out.println(Thread.currentThread().getName() + "is an actor");
System.out.println(Thread.currentThread().getName() + "show begins" + (++count));
System.out.println(Thread.currentThread().getName() + "show is over");
?}
Actor是否實(shí)現(xiàn)了Runable或繼承了Thread,run方法是否有問題
慕神1905738 提問者
查看下 Actor類 是否實(shí)現(xiàn)了 runable接口
舉報(bào)
帶你一起深入淺出多線程,掌握基礎(chǔ),展望進(jìn)階路線
3 回答Thread actor=new Actor(); 這怎么理解?
2 回答new Thread
4 回答Thread actressThread = new Thread(new Actress(),"Ms. Runnable");
1 回答Thread actressthread=new Thread(new Actress(),"Ms.Runnable");報(bào)錯(cuò)啊
2 回答new一個(gè)Actor類的對(duì)象賦給Thread類型的引用和賦給Actor類型的引用,效果是一樣的
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號(hào)-11 京公網(wǎng)安備11010802030151號(hào)
購(gòu)課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動(dòng)學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號(hào)
2016-10-25
package com.imooc.concurrent;
public class Actor extends Thread {
public void run() {
System.out.println(getName() + "is an actor");
int count = 0;
boolean a=true;
while (a){
System.out.println(getName() + "show begins" + (++count));
if (count==100)
{
a=false;
}
if(count%10==0){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// System.out.println(getName() + "show begins" + (++count));
System.out.println(getName() + "show is over");
}
public static void main(String[] args) {
// Thread actor = new Actor();
//
// actor.setName("Mr.Thread");
Thread actor=new Thread(new Actor(),"Mr.Thread");
actor.start();
Thread actressThread= new Thread(new Actress(),"Ms.Runnable");
actressThread.start();
}
}
?class Actress implements Runnable{
public void run() {
System.out.println(Thread.currentThread().getName() + "is an actor");
int count = 0;
boolean a=true;
while (a){
System.out.println(Thread.currentThread().getName() + "show begins" + (++count));
if (count==100)
{
a=false;
}
if(count%10==0){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// System.out.println(getName() + "show begins" + (++count));
System.out.println(Thread.currentThread().getName() + "show is over");
}
?}
2016-10-25
Actor是否實(shí)現(xiàn)了Runable或繼承了Thread,run方法是否有問題
2016-10-25
查看下 Actor類 是否實(shí)現(xiàn)了 runable接口