課程
/后端開發
/Java
/深入淺出Java多線程
為什么如題的代碼運行結果不對 編譯器也不報錯呢
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是否實現了Runable或繼承了Thread,run方法是否有問題
慕神1905738 提問者
查看下 Actor類 是否實現了 runable接口
舉報
帶你一起深入淺出多線程,掌握基礎,展望進階路線
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");報錯啊
2 回答new一個Actor類的對象賦給Thread類型的引用和賦給Actor類型的引用,效果是一樣的
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
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是否實現了Runable或繼承了Thread,run方法是否有問題
2016-10-25
查看下 Actor類 是否實現了 runable接口