千鋒學習的第27天 執行緒

2021-10-03 18:59:36 字數 1605 閱讀 6375

千鋒馬海超

千鋒學習的第27天

一、 乙個單cpu的機器,如何同時執行多個執行緒?請簡述其原理

多執行緒等待os時間片,巨集觀並行,實際序列實現。

二、(執行緒的建立)有以下**。

public class example implements ruunable

}public static void main(string args)

}選擇正確答案(c

a**編譯失敗,因為ex2 run()無法獲得執行

b **編譯成功,存在3個可執行的執行緒

c **編譯成功,存在1個可執行的執行緒

三(執行緒的建立)有下面**

class example implements runnable

public void run(int limit)

}選擇正確答案(**a

a 編譯出錯**

b 執行時異常

c 正常編譯執行,輸出sleep

d正常編譯執行,但是沒有內容輸出

五(執行緒的建立)建立兩個執行緒,要求如下

1 乙個執行緒輸出100個1-26,另乙個執行緒輸出100個a-z

2乙個執行緒使用繼承thread類的寫法,另乙個使用執行緒實現runnable介面的寫法

public

class

example

}class

numextends

thread}}

class

strimplements

runnable}}

在這裡插入**片

六(執行緒的同步)有如下**

class mythread1 extends thread

public void run()catch(exception e){}

system.out.println(「$$$」)}}

}}class mythread2 extends thread

public void run()catch(exception e){}

system.out.println(「###」);} }

}}public class testmythread

}問:在//1和//2處加上的synchronized起什麼作用?如果不講synchronized,執行程式有什麼不同的地方

synchronized 保持執行緒同步,

如果沒有synchronized 就會競爭object公共資源造成資料不安全

七(執行緒同步)有如下**

class mythread extends threadcatch(exception e)

system.out.println(data);}}

}public class testmythread

}問:上述**輸出的結果是什麼(c

a, 先輸出100個hello,然後是100個world

b, 先輸出100個world,然後是100個hello

c, 執行緒不同步,因此交替輸出hello和world

千鋒學習的第21天 包裝類

千鋒馬海超 千鋒學習的20天包裝類 public class testinteger class adobjecr常用方法 public char charat in index 根據下標獲取字元 public boolean contains string str 判斷當前字串中是否包含了str ...

千鋒逆戰班,陳強 在千鋒「逆戰」學習第 22天

string字串的常用方法 1.tostring 返回物件本身 2.substring 陣列的下標 返回乙個新的字串,它是此字串的乙個子字串。從括號定義的下標開始 3.touppercase 將字串中的字母全部變成大寫 4.tolawercase 將字串中的字母全部變成小寫 5.trim 去掉字串前...

千鋒逆戰班,陳強 在千鋒「逆戰」學習第 23天

一 bigdecimal類 用於處理高精度計算 的常用方法 1.bigdecimal常用的幾個構造方法 1 bigdecimal int 將int表示形式轉換成bigdecimal物件 2 bigdecimal string 將字串表示形式轉換成bigdecimal物件 3 bigdecimal d...