1.代碼實作單例模式中的懶漢模式
/**
* 單例模式中的懶漢模式
*/
public class LazySingleton {
/**
* 用volatile 修飾類變數
*/
private static volatile LazySingleton singleton;
/**
* 構造器私有
*/
private LazySingleton(){}
/**
* 雙重檢查
* @return
*/
public static LazySingleton getInstance(){
if(singleton == null){
synchronized (LazySingleton.class){
if(singleton == null){
singleton = new LazySingleton();
}
}
}
return singleton;
}
/**
* 測驗
*/
public static void main(String[] args) {
LazySingleton singleton1 = LazySingleton.getInstance();
LazySingleton singleton2 = LazySingleton.getInstance();
System.out.println("singleton1:"+singleton1);
System.out.println("singleton2:"+singleton2);
System.out.println(singleton1 == singleton2);
}
}
2.撰寫一個Java程式在螢屏上輸出1!+2!+3!+...+10!的和
public class Factorial {
/**
* 用遍歷的方式解題
*/
public static void traverseMethod() {
int i, j, mul, sum = 0;
for (i = 1; i <= 10; i++) {
mul = 1;
for (j = 1; j <= i; j++) {
mul = mul * j;
}
sum = sum + mul;
}
System.out.println("1!+2!+3!+...+10!=" + sum);
}
/**
* 用遞回的方式解題
*/
public static void factorialMethod() {
System.out.println("1!+2!+3!+...+10!="
+ (factorial(1) + factorial(2)
+ factorial(3) + factorial(4)
+ factorial(5) + factorial(6)
+ factorial(7) + factorial(8)
+ factorial(9) + factorial(10))
);
}
public static int factorial(int num) {
if (num == 1) {
return 1;
}
return num * factorial(num - 1);
}
/**
* 測驗
*/
public static void main(String[] args) {
traverseMethod();//4037913
factorialMethod();//4037913
}
}
3.設計一個生產電腦和搬運電腦類,要求生產一臺電腦就搬走一臺電腦,如果沒有新的電腦生產出來,則搬運工要等待新電腦產出;如果生產出的電腦沒有搬走,則要等待電腦搬走之后在生產,并統計出生產的電腦數量,
public class ComputerTest {
public static void main(String[] args) throws InterruptedException {
Computer computer = new Computer();
Thread produceThread = new Thread(() -> {
for (int i = 0; i < 10; i++) {
try {
computer.produceComputer();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "produceThread");
produceThread.start();
Thread carryThread = new Thread(() -> {
for (int i = 0; i < 10; i++) {
try {
computer.carryComputer();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "carryThread");
carryThread.start();
produceThread.join();
carryThread.join();
System.out.printf("共生產了%s臺電腦\n", computer.getComputerCount());
}
/**
* 電腦類
*/
public static class Computer {
/**
* 生產出的電腦的數量
*/
private int computerCount;
//電腦是否不為空 true-不為空 false為空
private volatile boolean flag;
/**
* 獲取生產出的電腦的數量
*
* @return
*/
public int getComputerCount() {
return computerCount;
}
/**
* 生產電腦
*/
public synchronized void produceComputer() throws InterruptedException {
while (flag) {
wait();
}
this.computerCount++;
flag = true;
System.out.printf("%s 開始生產電腦\n", Thread.currentThread().getName());
notifyAll();
}
/**
* 搬運電腦
*/
public synchronized void carryComputer() throws InterruptedException {
//讓搬運電腦消耗1秒鐘,看程式執行效果
TimeUnit.SECONDS.sleep(1);
while (!flag) {
wait();
}
flag = false;
System.out.printf("%s 開始搬運電腦\n", Thread.currentThread().getName());
notifyAll();
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/167796.html
標籤:Java
上一篇:做站外包給三方需要注意哪些問題
下一篇:int和Integer的默認值
