7장 - 자바 날개 달기(7-4, 7-5, 7-6)
ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ
7-4 예외처리
1) finally
// 7-4 finally
public class Sample{
public void shouldBeRun(){
System.out.println("ok thanks.");
}
public static void main(String[] args){
Sample sample = new Sample();
int c;
try {
c = 4 / 0;
//sample.shouldBeRun();
}catch (ArithmeticException e){
c = -1;
} finally{
sample.shouldBeRun();
}
}
}
2) RuntimeException과 Exception
//7-4 RuntimeException 과 Exception
class FoolException extends Exception{
}
public class Sample{
public void sayNick(String nick){
try {
if ("fool".equals(nick)) {
throw new FoolException();
}
System.out.println("당신의 별명은 " + nick + " 입니다.");
}catch(FoolException e){
System.err.println("FoolException이 발생했습니다.");
}
}
public static void main(String[] args){
Sample sample = new Sample();
sample.sayNick("fool");
sample.sayNick("genious");
}
}
3)
ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ
7-5 쓰레드
1) Thread
//7-5 Thread
public class Sample extends Thread{
int seq;
public Sample(int seq){
this.seq = seq;
}
public void run(){
System.out.println(this.seq + " thread start.");
try{
Thread.sleep(1000);
}catch (Exception e){
}
System.out.println(this.seq + " thread end");
}
public static void main(String[] args){
for(int i=0; i<10; i++){
Thread t = new Sample(i);
t.start();
}
System.out.println("main end.");
}
}
2) Join
//7-5 Join
import java.util.ArrayList;
public class Sample extends Thread{
int seq;
public Sample(int seq){
this.seq = seq;
}
public void run(){
System.out.println(this.seq + " thread start.");
try{
Thread.sleep(1000);
}catch (Exception e){
}
System.out.println(this.seq + " thread end");
}
public static void main(String[] args){
ArrayList<Thread> threads = new ArrayList<>();
for(int i=0; i<10; i++){
Thread t = new Sample(i);
t.start();
threads.add(t);
}
for(int i=0; i<threads.size(); i++){
Thread t = threads.get(i);
try{
t.join();
}catch (Exception e){
}
}
System.out.println("main end.");
}
}
3) Runnable
//7-5 Runnable
import java.util.ArrayList;
import java.util.concurrent.TransferQueue;
public class Sample implements Runnable{
int seq;
public Sample(int seq){
this.seq = seq;
}
public void run(){
System.out.println(this.seq + " thread start.");
try{
Thread.sleep(1000);
}catch (Exception e){
}
System.out.println(this.seq + " thread end");
}
public static void main(String[] args){
ArrayList<Thread> threads = new ArrayList<>();
for(int i=0; i<10; i++){
Thread t = new Thread(new Sample(i));
t.start();
threads.add(t);
}
for(int i=0; i<threads.size(); i++){
Thread t = threads.get(i);
try{
t.join();
}catch (Exception e){
}
}
System.out.println("main end.");
}
}
ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ
7-6 함수형 프로그래밍
1) Lambda
// 7-6 함수형 프로그래밍 - Lambda
@FunctionalInterface
interface Calculator{
int sum(int a, int b);
}
public class Sample{
public static void main(String[] args){
Calculator mc = (int a, int b) -> a +b;
int result = mc.sum(3,4);
System.out.println(result);
}
}
2) Stream
// 7-6 Stream 적용 전
import java.util.Arrays;
import java.util.Comparator;
public class Sample{
public static void main(String[] args){
int[] data = {5, 6, 4, 2, 3, 1, 1, 2, 2, 4, 8};
int[] result = Arrays.stream(data)
.boxed()
.filter((a) -> a%2 == 0)
.distinct()
.sorted(Comparator.reverseOrder())
.mapToInt(Integer::intValue)
.toArray()
;
}
}
// 7-6 Stream 적용 후
import java.util.*;
public class Sample{
public static void main(String[] args){
int[] data = {5, 6, 4, 2, 3, 1, 1, 2, 2, 4, 8};
ArrayList<Integer> dataList = new ArrayList<>();
for(int i=0; i<data.length; i++){
if(data[i] % 2 == 0){
dataList.add(data[i]);
}
}
HashSet<Integer> dataSet = new HashSet<>(dataList);
ArrayList<Integer> distinctList = new ArrayList<>(dataSet);
distinctList.sort(Comparator.reverseOrder());
int[] result = new int[distinctList.size()];
for(int i=0; i< distinctList.size(); i++){
result[i] = distinctList.get(i);
}
}
}
ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ
'점프 투 자바(Java) > 23년 1월' 카테고리의 다른 글
2월 1일(수) - 8장 (0) | 2023.02.01 |
---|---|
1월 27일(금) - 7장 연습문제 (0) | 2023.01.27 |
1월 25일(수) - 7장(자바 날개 달기) (0) | 2023.01.25 |
1월 20일(금) - 6장(입출력) (0) | 2023.01.20 |
1월 19일(목) - 5장 연습문제 (0) | 2023.01.19 |
댓글