JAVA 다 중 스 레 드 디자인 모델 11 Thread - Specific Storage Pattern
2392 단어 자바 다 중 스 레 드
public class Log {
private static final ThreadLocal tsLogCollection = new ThreadLocal();
// log
public static void println(String s) {
getTSLog().println(s);
}
// log
public static void close() {
getTSLog().close();
}
// log
private static TSLog getTSLog() {
TSLog tsLog = (TSLog)tsLogCollection.get();
// , log
if (tsLog == null) {
tsLog = new TSLog(Thread.currentThread().getName() + "-log.txt");
tsLogCollection.set(tsLog);
}
return tsLog;
}
}
public class TSLog {
private PrintWriter writer = null;
// writer
public TSLog(String filename) {
try {
writer = new PrintWriter(new FileWriter(filename));
} catch (IOException e) {
e.printStackTrace();
}
}
// log
public void println(String s) {
writer.println(s);
}
// log
public void close() {
writer.println("==== End of log ====");
writer.close();
}
}
public class ClientThread extends Thread {
public ClientThread(String name) {
super(name);
}
public void run() {
System.out.println(getName() + " BEGIN");
for (int i = 0; i < 10; i++) {
Log.println("i = " + i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
}
Log.close();
System.out.println(getName() + " END");
}
}
public class Main {
public static void main(String[] args) {
new ClientThread("Alice").start();
new ClientThread("Bobby").start();
new ClientThread("Chris").start();
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
JAVA 다 중 스 레 드 다운로드 및 정지점 전송정지점 전송 과 다 중 스 레 드 다운로드 원 리 는 같 습 니 다. http2.setRequestProperty("RANGE","bytes="+startl+"-");//정지점 위 치 를 설정 하고 서버 에 파일 의...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.