Skip to content

Instantly share code, notes, and snippets.

@jungbin-kim
Last active September 5, 2018 03:56
Show Gist options
  • Save jungbin-kim/397d65733b32e41e7b899d2222e512c1 to your computer and use it in GitHub Desktop.
Save jungbin-kim/397d65733b32e41e7b899d2222e512c1 to your computer and use it in GitHub Desktop.
자바 공부

CompletableFuture

thenApply 메소드는 비동기 연산 결과로 넘어온 값을 입력으로 받고 처리 후 반환하는 함수를 입력으로 받는다.

	@Test
	public void TestFuture() {

		CompletableFuture<String> completableFuture 
		  = CompletableFuture.supplyAsync(() -> {
			  try {
				  Thread.sleep(3000);
			  } catch (InterruptedException e) {
				  e.printStackTrace();
			  }
			  return "Hello";  
		  });
		 
		CompletableFuture<Void> future = completableFuture
		  .thenAcceptAsync(s -> System.out.println("Computation finished: " + s));
		 
		try {
			future.get();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
	}

mvn cli 로 프로젝트 생성

$ mvn archetype:generate 또는 $ mvn archetype:generate -DarchetypeArtifactId=maven-archetype-quickstart

groupId: 프로젝트를 생성하는 개인/단체를 의미하는 ID artifactId: App 이름

mvn 과 jenv(자바 버전 관리자) 연동

$ jenv enable-plugin maven 출처: jenv/jenv#78 (comment)

git 적용

$ git init $ git add . $ git commit -m "Init commit"

packaging 설정 적용

File io with exception handling

BufferedReader bufferedReader = null;
try {
	// Reading the file
	bufferedReader = new BufferedReader(new FileReader("file path"));

	System.out.println("진행");
	String line = "";
	while ((line = bufferedReader.readLine()) != null) {
	System.out.println(line);
	}
} catch(FileNotFoundException e) {
	// TODO file path에 File 이 없을 때 에러
	System.out.println(e.getMessage());	
} catch (IOException e) {
	// TODO 버퍼의 readLine() 시 에러
	e.printStackTrace();
} finally {
	if(bufferedReader != null) {
		try {
			bufferedReader.close();
		} catch(IOException ie) {
			System.out.println("Error occured while closing the BufferedReader");
			ie.printStackTrace();
    	}	
	}
}

참고

public void closeResourceInFinally() {
	FileInputStream inputStream = null;
	try {
		File file = new File("./tmp.txt");
		inputStream = new FileInputStream(file);
		
		// use the inputStream to read a file
		
	} catch (FileNotFoundException e) {
		log.error(e);
	} finally {
		if (inputStream != null) {
			try {
				inputStream.close();
			} catch (IOException e) {
				log.error(e);
			}
		}
	}
}

자바 프로젝트 세팅

환경: Eclipse photon

Eclipse에 JUnit 설정하기

  1. Java Project를 생성
  2. Project 에서 마우스 우클릭 > Properties
  3. Java BuildPath를 선택
  4. Libraries 탭을 선택하고, Add Library를 선택

출처: 새내기 개발자의 JUnit 여행기

Eclipse에 git plugin 설치

Help > Install New Software > Work with 에 http://download.eclipse.org/egit/updates 입력 > git 플러그인 설치

출처: 이클립스와 GITHUB 연동하기

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment