題目1
撰寫程式,創建兩個執行緒物件,一根執行緒回圈輸出“播放背景音樂”,另一根執行緒回圈輸出“顯示畫面”;
要求:
1: 1個執行緒使用Runnable介面的匿名內部類實作
2: 另一個執行緒使用lambda實作
效果:

參考答案:
public static void main(String[] args) {
//匿名內部類
new Thread(new Runnable() {
@Override
public void run() {
while (true){
System.out.println("播放背景音樂...");
}
}
}).start();
//lambda
new Thread(()->{
while (true){
System.out.println("顯示畫面...");
}
}).start();
}
題目2
3.請使用繼承Thread類的方式定義一個執行緒類,在run()方法中回圈10次,每1秒回圈1次,每次回圈按“yyyy-MM-dd HH:mm:ss”的格式列印當前系統時間,
請定義測驗類,并定義main()方法,啟動此執行緒,觀察控制臺列印,
要求:
1: 使用匿名內部類配合SimpleDateFormat和Date實作
2: 使用lambda配合LocalDateTime和DateTimeFormatter實作
效果:

參考答案:
//方式1:
public static void main(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
new Thread(){
@Override
public void run() {
for (int i = 0; i < 10; i++) {
String format = sdf.format(new Date());
System.out.println(format);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
//方式2:
public static void main(String[] args) {
DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
new Thread(()->{
for (int i = 0; i < 10; i++) {
String format = f.format(LocalDateTime.now());
System.out.println(format);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
題目3
請撰寫多執行緒應用程式,模擬多個人通過一個山洞:
(1).這個山洞每次只能通過一個人,每個人通過山洞的時間為1秒;
(2).創建10個執行緒,同時準備過此山洞,并且定義一個變數用于記錄通過隧道的人數,顯示每次通過山洞人的姓名,和通過順序;
要求:
保證安全問題,不能出現多個人同時通過山洞的現象;(必須逐一通過)
效果:

參考答案:
public class A {
public static void main(String[] args) {
MyRunnable mr = new MyRunnable();
for (int i = 0; i < 10; i++) {
new Thread(mr,"執行緒"+i).start();
}
}
}
class MyRunnable implements Runnable {
private int count = 1;
public void run() {
synchronized (this) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"通過山洞,他是第"+count+"個");
count++;
}
}
}
題目4
拼手速抽獎案例.
1.現有一個集合裝了10個獎品在里面,分別是:{“電視機”,“電冰箱”,“電腦”,“游戲機”,“洗衣機”,“空調”,“手機”,“平板電腦”,“電動車”,“電飯煲”};
2.假如有3個人同時去抽這10個獎品.最后列印出來.三個人各自都抽到了什么獎品.
例如:
張三: “電視機”,”電冰箱”,”電腦”,”游戲機”,”洗衣機”
李四: ”空調”,”手機”,”平板電腦”,
王五: ”電動車”,”電飯煲
要求:
1:3個人同時開始抽獎,每次抽獎需要使用0.5秒才能完成抽獎;
2:需要控制住同一個獎項不能同時被多個人抽走;
效果:

參考答案:
public class A {
public static void main(String[] args) {
//創建抽獎任務類物件
MyRunnable mr = new MyRunnable();
//創建3個執行緒物件,都關聯著任務物件,
Thread t1 = new Thread(mr,"張三");
Thread t2 = new Thread(mr,"李四");
Thread t3 = new Thread(mr,"王五");
//開始抽獎
t1.start();
t2.start();
t3.start();
}
}
class MyRunnable implements Runnable {
private static ArrayList<String> list = new ArrayList<>();
static {
//初始化獎品,而且,只能初始化1次,所以使用了靜態代碼塊
Collections.addAll(list,"電視機","電冰箱","電腦","游戲機","洗衣機","空調","手機","平板電腦","電動車","電飯煲");
}
public void run() {
while (list.size()>0){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (this) {
if(list.size()==0){
return;//加這個if是確保安全的
}
String r = list.remove(new Random().nextInt(list.size()));
System.out.println(Thread.currentThread().getName()+"抽到了:"+r);
}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/243966.html
標籤:其他
上一篇:2021,新開始,新起點
下一篇:資訊安全復習
