#10 effective java: item 7
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
package com.example.effectivejava.chapter01.item07.executor;
|
||||
|
||||
import java.util.concurrent.*;
|
||||
|
||||
public class ExecutorsExample {
|
||||
|
||||
public static void main(String[] args) throws ExecutionException, InterruptedException {
|
||||
ExecutorService service = Executors.newFixedThreadPool(10);
|
||||
|
||||
Future<String> submit = service.submit(new Task());
|
||||
|
||||
System.out.println(Thread.currentThread() + " hello");
|
||||
|
||||
System.out.println(submit.get());
|
||||
|
||||
service.shutdown();
|
||||
}
|
||||
|
||||
static class Task implements Callable<String> {
|
||||
|
||||
@Override
|
||||
public String call() throws Exception {
|
||||
Thread.sleep(2000L);
|
||||
return Thread.currentThread() + " world";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.example.effectivejava.chapter01.item07.optional;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class Channel {
|
||||
|
||||
private int numOfSubscribers;
|
||||
|
||||
public Optional<MemberShip> defaultMemberShip() {
|
||||
if (this.numOfSubscribers < 2000) {
|
||||
return Optional.empty();
|
||||
} else {
|
||||
return Optional.of(new MemberShip());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.example.effectivejava.chapter01.item07.optional;
|
||||
|
||||
public class MemberShip {
|
||||
|
||||
public String hello() {
|
||||
return "hello";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.example.effectivejava.chapter01.item07.reference;
|
||||
|
||||
public class BigObject {
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.example.effectivejava.chapter01.item07.reference;
|
||||
|
||||
import java.lang.ref.PhantomReference;
|
||||
import java.lang.ref.ReferenceQueue;
|
||||
|
||||
public class BigObjectReference<BigObject> extends PhantomReference<BigObject> {
|
||||
|
||||
public BigObjectReference(BigObject referent, ReferenceQueue<? super BigObject> q) {
|
||||
super(referent, q);
|
||||
}
|
||||
|
||||
public void cleanUp() {
|
||||
System.out.println("clean up");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.example.effectivejava.chapter01.item07.reference;
|
||||
|
||||
import java.lang.ref.Reference;
|
||||
import java.lang.ref.ReferenceQueue;
|
||||
|
||||
public class PhantomReferenceExample {
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
BigObject strong = new BigObject();
|
||||
ReferenceQueue<BigObject> rq = new ReferenceQueue<>();
|
||||
|
||||
BigObjectReference<BigObject> phantom = new BigObjectReference<>(strong, rq);
|
||||
strong = null;
|
||||
|
||||
System.gc();
|
||||
Thread.sleep(3000L);
|
||||
|
||||
// 사라지진 않고 큐에 들어갑니다.
|
||||
System.out.println(phantom.isEnqueued());
|
||||
|
||||
Reference<? extends BigObject> reference = rq.poll();
|
||||
BigObjectReference bigObjectCleaner = (BigObjectReference) reference;
|
||||
bigObjectCleaner.cleanUp();
|
||||
reference.clear();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.example.effectivejava.chapter01.item07.reference;
|
||||
|
||||
import java.lang.ref.SoftReference;
|
||||
|
||||
public class SoftReferenceExample {
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
Object strong = new Object();
|
||||
SoftReference<Object> soft = new SoftReference<>(strong);
|
||||
strong = null;
|
||||
|
||||
System.gc();
|
||||
Thread.sleep(3000L);
|
||||
|
||||
// 메모리가 충분해서.. 굳이 제거할 필요가 없어서 잘 안지워짐;;
|
||||
System.out.println(soft.get());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.example.effectivejava.chapter01.item07.reference;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
|
||||
public class WeakReferenceExample {
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
Object strong = new Object();
|
||||
WeakReference<Object> weak = new WeakReference<>(strong);
|
||||
strong = null;
|
||||
|
||||
System.gc();
|
||||
Thread.sleep(3000L);
|
||||
|
||||
System.out.println(weak.get());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.example.effectivejava.chapter01.item07.optional;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class ChannelTest {
|
||||
|
||||
@Test
|
||||
void npe() {
|
||||
Channel channel = new Channel();
|
||||
Optional<MemberShip> optional = channel.defaultMemberShip();
|
||||
optional.ifPresent(MemberShip::hello);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user