java
-
Java - Deamon Thread, Thread GroupJava 2018. 1. 31. 13:14
// deamon thread는 main thread작업을 돕는 보조적인 역할을 수행 한다.// Work Thread와 마찬가지로 main thread에 의해서 생성 된다// 다만 life cycle의 차이가 있다. // deamon thread는 main thread가 끝날때 같이 끝난다.// Work thread는 작업이 다 끝나야 thread가 끝난다. public class DeamonThread extends Thread { @Override public void run() { while(true) { try { Thread.sleep(500); System.out.println("Thread Working..."); } catch (InterruptedExcept..
-
Java - Wait, NotifyJava 2018. 1. 31. 10:38
public class CommonData { private String data; //synchronized 는 차후 다수의 스레드가 접근할 여지가 있는지 확인 해 볼것 public synchronized void getData() { try { //1. 데이터가 있는지 확인 if(data != null) { //2. 있으면 가져오고 없으면 가져오지 않는다. System.out.println("가져 온 값 : "+data); data = null;//가져온 후 값을 비워 준다. } //3. 상대를 깨우고 나는 쉰다. notify(); wait(); } catch (InterruptedException e) { e.printStackTrace(); } } public synchronized void se..
-
Java - Stack, QueueJava 2018. 1. 25. 13:12
// Stack - LIFO// 마지막에 들어온 최신 것을 이용할 때 유용 // Queue - FIFO// 순차적으로 작업 처리할 때 유용 import java.util.Stack; public class TowelBox { public static void main(String[] args) { // Stack -> LIFO Stack box = new Stack(); // push box.push(new Towel("red")); box.push(new Towel("orange")); box.push(new Towel("yellow")); box.push(new Towel("green")); box.push(new Towel("blue")); box.push(new Towel("na..
-
Java - CollectionJava 2018. 1. 25. 10:42
// 같은 Interface라 메소드는 비슷하다. // Array List는 index로 객체를 관리한다는 점에서 Array와 유사하다// 차이점은 Array와 달리 index가 유연하다는 점이다// Array List는 객체 삭제/추가 시 inedx가 1씩 당겨지거나 미뤄지게 된다// 때문에 빈번한 객체의 추가, 삭제가 일어날 때 다소 무리가 된다.// Collection Framework에서 가장 많이 쓰인다.// ArrayList 사용, Generic 가능// 선언시 크기 지정을 할 수 있지만 안해도 상관없다. ex) ArrayList list = new ArrayList(3); // 크기 지정 ArrayList list = new ArrayLis..
-
Java - Generic MethodJava 2018. 1. 24. 10:47
// Generic은 Type의 자유 public class Box { private T t; public T getT() { return t; } public void setT(T t) { this.t = t; } } public class Gmethod { // 매개변수 사용 public void method01(T t) { System.out.println("입력 값 : "+t); } // 매개변수, 반환타입 사용 public T method2(T t) { return t; } // 활용 public Box method3(T t){ // 반환 값이 Box 타입이기 때문에 메소드 타입에 Box 사용 Box box = new Box(); box.setT(t); retu..
-
Java - GenericJava 2018. 1. 24. 08:44
Generic을 사용하면 다시 원형으로 돌아가기 위한 Casting절차를 없앨 수 있다. public class Box { private Object value; public Object getValue() { return value; } public void setValue(Object value) { this.value = value; } } public class Main { public static void main(String[] args) { Box box = new Box(); // Box안에는 무엇이 들어갈ㅈ ㅣ몰라서 Object로 선언 box.setValue(1234); int value = (int) box.get..
-
Java - StringJava 2018. 1. 23. 11:10
public class Main { public static void main(String[] args) { // 문자열 만드는 방법 String str1 = "abcdefg"; System.out.println(str1); String str2 = new String("asdasd"); System.out.println(str2); char[] alpha = {'a','b','c','d','e','f'}; String str3 = new String(alpha); System.out.println(str3); } } public class StringUtil { public static void main(String[] args) { String string = "abcdef..
-
Java - Try, CatchJava 2018. 1. 23. 08:58
public class Main { public static void main(String[] args) { String data1 = "12345"; String data2 = "asdfg"; try {//예외 발생이 예상되는 지점 int val1 = Integer.parseInt(data1); int val2 = Integer.parseInt(data2); }catch(NumberFormatException e) {//NumberFormatException 발생시 실행 할 코드 System.out.println("입력된 값에 문자가 있는지 확인 해 주세요"); System.out.println(e.toString()); }finally {//예외 발생에 상관 없이 무조건 실행 System.out.pr..