π 2023λ 12μ 27μΌ
[java 16μΌμ°¨]
μ€νΈλ¦Ό(stream)
μλ°μμλ νμΌμ΄λ μ½μμ μ μΆλ ₯μ μ§μ λ€λ£¨μ§μκ³ , μ€νΈλ¦Όμ μ΄μ©νλ€.
λμμ μννλ €λ©΄ μ λ ₯μ€νΈλ¦Όκ³Ό μΆλ ₯μ€νΈλ¦Ό λͺ¨λ νμνκ³ ν(queue)μ κ°μ FIFO κ΅¬μ‘°λ‘ μ΄λ£¨μ΄μ Έ μλ€.
file
/* λ²νΌ κΈ°λ₯μ΄ κ΅¬νλμ΄μκ³ , nio ν¨ν€μ§μμλ non-blocking λ°©μ μ¬μ© κΈ°λ³Έμ΄ StandardCharsets.UTF_8 μ΄λ€. */ Path filePath = Paths.get("src/C17_Exception_File_Parsing/text_file.txt" try { if(Files.exists(filePath)){ Files.write(filePath, "μν₯λ―Ό\n".getBytes(), StandardOpenOption.WRITE); }else{ Files.write(filePath, "μν₯λ―Ό\n".getBytes(), StandardOpenOption.CREATE_NEW); } }catch (IOException e){ e.printStackTrace(); }
// νμΌ μΌκΈ° : readString , readAllLines(List νν) try { String str = Files.readString(filePath); System.out.println(str); }catch (IOException e){ e.printStackTrace(); } System.out.println(); try { List<String> stringList = Files.readAllLines(filePath); for(String str :stringList){ System.out.println(str); } }catch (IOException e) { e.printStackTrace(); }
- json
// http ν΄λΌ μμ± HttpClient client = HttpClient.newHttpClient(); ObjectMapper mapper = new ObjectMapper(); // http μμ²κ°μ²΄ μμ± HttpRequest request = HttpRequest.newBuilder() .uri(URI.create("https://jsonplaceholder.typicode.com/posts/1")) .GET() .build(); HttpRequest request1 = HttpRequest.newBuilder() .uri(URI.create("https://jsonplaceholder.typicode.com/posts")) .GET() .build(); // http μλ΅ κ°μ²΄ μμ± try { HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); JsonNode node = mapper.readTree(response.body()); Post post = new Post( node.get("userId").asInt(), node.get("id").asInt(), node.get("title").asText(), node.get("body").asText() ); Post post1 = mapper.readValue(response.body(),Post.class); /* json Nodeλ νΈλ¦¬κ΅¬μ‘° μ΄λ―λ‘, for(JsonNode a : jsonNode) νμμΌλ‘ μ¬μ©κ°λ₯ */ HttpResponse<String> response1 = client.send(request1, HttpResponse.BodyHandlers.ofString()); ArrayList<Post> postArrayList = new ArrayList<>(); JsonNode node1 = mapper.readTree(response1.body()); for(JsonNode a : node1){ // Post post3 = new Post( // a.get("userId").asInt(), // a.get("id").asInt(), // a.get("title").asText(), // a.get("body").asText() // ); Post post3 = mapper.readValue(a.toString(),Post.class); postArrayList.add(post3); } System.out.println(postArrayList); // java κ°μ²΄λ₯Ό json λ°μ΄ν°λ‘ μ λ ¬ν String serialized_data = mapper.writeValueAsString(postArrayList); System.out.println(serialized_data); } catch (IOException | InterruptedException e) { throw new RuntimeException(e); }
/* Thread ν΄λμ€μ μ΄λ―Έ ꡬνλμ΄ μλ run λ©μλλ μ무μμ λ νμ§ μλ λΉ λ©μλ μμ νκ³ μΆμ λ΄μ©μ run() λ©μλλ₯Ό overriding νμ¬ μ μν μ μλ€. μμ‘κ΄κ³μ΄λ€ 보λ, λ€λ₯Έ ν΄λμ€ μμ λΆκ° */ public class ExtendsThreadClass extends Thread{ // run λ©μλλ μ°λ λκ° μμλλ©΄ μ€ν @Override public void run(){ System.out.println("ExtendsThreadClass : " + Thread.currentThread().getName()); } }
public class RunnableImplementsClass implements Runnable { @Override public void run() { System.out.println("RunnableImplementsClass : "+ Thread.currentThread().getName()); } }
public class Library { static int bookCount = 100; // public static void barrowBook(){ // if(bookCount>0){ // try{ // Thread.sleep(10); // } catch (InterruptedException e) { // throw new RuntimeException(e); // } // // // μ± λΉλ¦¬λ μκ° // bookCount -= 1; // System.out.println("λμΆ μλ£"); // System.out.println("λ¨μμλ μλ: " +bookCount); // }else{ // System.out.println("λμΆ λΆκ°"); // } // } // synchronized ν€μλλ₯Ό ν΅ν΄ ν΄λΉ λ©μλ νν΄μλ lockκ±Έλλ‘ μ€μ public synchronized static void barrowBook(){ if(bookCount>0){ try{ Thread.sleep(10); } catch (InterruptedException e) { throw new RuntimeException(e); } // μ± λΉλ¦¬λ μκ° bookCount -= 1; System.out.println("λμΆ μλ£"); System.out.println("λ¨μμλ μλ: " +bookCount); }else{ System.out.println("λμΆ λΆκ°"); } } }
public class MainClass { public static void main(String[] args) { Thread etc1 = new ExtendsThreadClass(); etc1.start(); Thread etc2 = new ExtendsThreadClass(); etc2.start(); Thread etc3 = new ExtendsThreadClass(); etc3.start(); // μ€λ λ μ€ν μ μμ°¨μ μΌλ‘ μ€νλμ§ μμμ μ μ // μ€λ λ μμ±μλ‘ Runnable κ°μ²΄κ° μ λ¬λμ΄ μ¬μ©μκ° μ§μ μ μ run λ©μλκ° μ€ν new Thread(new RunnableImplementsClass()).start(); new Thread(new Runnable() { @Override public void run() { System.out.println("μ΅λͺ κ°μ²΄ μ€λ λ"); } }).start(); new Thread(() -> System.out.println("μ΅λͺ κ°μ²΄ λλ€ μ€λ λ")).start(); // μ€λ λμ λμμ± μ΄μ ν μ€νΈ // λ¨μΌ μ€λ λ μΌλ°νΈμΆ for (int i =0; i<1000; i++){ Thread th = new Thread(Library::barrowBook); th.start(); // joinλ©μλλ₯Ό ν΅ν΄ λ€λ₯Έ μ€λ λμ μλ£μ κΉμ§ μλ‘μ΄ μ€λ λκ° μ€νλμ§ μλλ‘ λ§μ. // try { // th.join(); // } catch (InterruptedException e) { // e.printStackTrace(); // } } System.out.println("μ΅μ’ λ¨μ μλ "+ Library.bookCount); } }
- νλ‘μΈμ€μ μ€λ λ
νλ‘μΈμ€ : cpuμ μν΄ λ©λͺ¨λ¦¬μ μ¬λ €μ Έ μ€νμ€μΈ νλ‘κ·Έλ¨
μ€λ λ : μ€μ§μ μΌλ‘ μμ μ μ€ννλ λ¨μ
νλ‘μΈμ€μλ μ μ΄λ νκ° μ΄μμ μ€λ λκ° μμΌλ©°, Main μ€λ λ νλλ‘ μμνμ¬ μ€λ λλ₯Ό μΆκ° μμ±νκ² λλ©΄ λ©ν° μ€λ λ νκ²½μ΄ λλ€.- Thread ν΄λμ€λ₯Ό μμλ°λ λ°©λ²κ³Ό λ€λ₯Έ νλλ Runnable μΈν°νμ΄μ€λ₯Ό ꡬννλ λ°©λ².
- λ©ν°μ€λ λμ λμμ±λ¬Έμ λ₯Ό javaμμ join() λλ syncronizedμΌλ‘ ν΄κ²°.
- join() : ν μ€λ λκ° μλ£λ λκΉμ§ λ€λ₯Έ μ€λ λμ startλ₯Ό νμ§ μλλ‘ ν λ μ¬μ©
- syncronized: race conditionμ΄ λ°μν μ μλ μ½λ μμμ 보νΈν λ μ¬μ©