-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
376 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package day11; | ||
|
||
public class Account { | ||
|
||
private int money=10; | ||
private boolean flag = false; | ||
|
||
//synchronized를 붙이면 해당 객체의 lock을 쥐어야만 해당 블럭을 수행할 수 있음 | ||
public void save(int val) { | ||
synchronized(this) { | ||
if(flag) { | ||
try { | ||
wait(); | ||
}catch(InterruptedException e) {} | ||
} | ||
money+=val; | ||
System.out.printf("입금액: %d, 입금후 잔액: %d%n", val, money); | ||
flag=true; | ||
notify(); | ||
//wait pool에 대기 중인 스레드 하나를 깨워서 runnable한 상태로 | ||
//전환시킨다. notifyAll() => 대기 중인 스레드 모두를 깨움 | ||
}// 동기화 블럭------------------------- | ||
|
||
} | ||
|
||
synchronized public void get(int val) { | ||
if(!flag) { | ||
try { | ||
wait(); | ||
// wait()가 호출되면 스레드는 수행권한을 포기하고 wait pool에 가서 대기한다. | ||
// 이 때 락을 반납하고 대기상태에 들어간다. | ||
}catch(InterruptedException e) {} | ||
|
||
}//---------------------if | ||
|
||
|
||
if(money-val <0) { | ||
System.out.printf("현금 부족!! 현재 잔액: %d, 요청 금액: %d%n", money, val); | ||
flag= false; | ||
notify(); | ||
return; | ||
} | ||
money-=val; | ||
System.out.printf("출금액: %d, 출금 후 잔액: %d%n", val, money); | ||
flag = false; | ||
notify(); | ||
} | ||
|
||
|
||
}/////////////////////////////////////// |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package day11; | ||
|
||
public class AccountTest { | ||
|
||
public static void main(String[] args) { | ||
|
||
Account ac = new Account(); | ||
|
||
UserIn u1 = new UserIn("개미", ac); | ||
UserOut u2 = new UserOut("베짱이", ac); | ||
|
||
u2.start(); | ||
u1.start(); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package day11; | ||
//추상메서드가 1개인 인터페이스는 함수형 인터페이스로 선언할 수 있다 | ||
//함수형인터페이스에 있는 메서드들은 람다식으로 구현할 수 있다. | ||
@FunctionalInterface | ||
interface MyNum{ | ||
int getMax(int a, int b); | ||
} | ||
|
||
public class LambdaTest { | ||
|
||
public static void main(String[] args) { | ||
MyNum my=(x,y)->{ | ||
if(x>=y) { | ||
return x; | ||
}else { | ||
return y; | ||
} | ||
}; | ||
|
||
int mx=my.getMax(11, 5); | ||
System.out.println(mx); | ||
|
||
MyNum my2=(x,y)->(x>=y)? x: y; | ||
System.out.println(my2.getMax(-8, 1)); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package day11; | ||
/*함수형 인터페이스를 선언하세요 | ||
* -문자열 2개를 매개변수로 받으면 두 문자열을 연결하여 반환하는 메서드로 만들려고 한다 | ||
* 인터페이스명: StringConcat | ||
* 추상메서드명: makeString(s1,s2) 4.3041 | ||
* -람다식 이용해서 구현해보기 | ||
* */ | ||
|
||
@FunctionalInterface | ||
interface StringConcat{ | ||
String makeString(String s1,String s2); | ||
} | ||
|
||
public class LambdaTest2 { | ||
|
||
public static void main(String[] args) { | ||
int local=100; //지역변수 | ||
StringConcat sc=(s1,s2)->{ | ||
//local=200; [x] | ||
//지역변수는 람다식 내부에서 변경하면 에러남. final변수 이므로 | ||
System.out.println("local: "+local); | ||
return s1+" "+s2; | ||
}; | ||
System.out.println(sc.makeString("Hello", "Lambda")); | ||
|
||
StringConcat sc2=(x,y)->{ | ||
return x.toUpperCase()+" "+y.toUpperCase(); | ||
}; | ||
System.out.println(sc2.makeString("hi", "java")); | ||
|
||
StringConcat sc3=(a,b)-> a.toLowerCase()+" "+b.toLowerCase(); | ||
System.out.println(sc3.makeString("GOOD", "Bye")); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package day11; | ||
//매개변수없고 반환값도 없는 람다식 | ||
|
||
@FunctionalInterface | ||
interface MyFunc{ | ||
void method(); | ||
} | ||
@FunctionalInterface | ||
interface YourFunc{ | ||
void method(int a); | ||
} | ||
|
||
@FunctionalInterface | ||
interface OurFunc{ | ||
int method(int a, int b); | ||
} | ||
|
||
public class LambdaTest3 { | ||
static Thread tr; | ||
public static void main(String[] args) { | ||
MyFunc m1=()->System.out.println("####"); | ||
m1.method(); | ||
|
||
YourFunc y=(x)->System.out.println("x: "+x); | ||
y.method(100); | ||
|
||
y= a->{ | ||
int res=a*5; | ||
System.out.println(res); | ||
}; | ||
y.method(10); | ||
|
||
y= a-> System.out.println(a-3); | ||
y.method(10); | ||
|
||
OurFunc o =(a,b)->(int)Math.pow(a, b); | ||
int z =o.method(2, 5); | ||
System.out.println(z); | ||
|
||
tr =new Thread(()-> { | ||
for(int i=0; i<5; i++) { | ||
System.out.println(tr.getName()+"달팽이가 기어갑니다"); | ||
try { | ||
Thread.sleep(100); | ||
}catch(Exception e) {} | ||
} | ||
}); | ||
tr.start(); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package day11; | ||
|
||
import javax.swing.*; | ||
import java.awt.*; | ||
import java.awt.event.*; | ||
/* [1] Runnable을 상속받아 스레드가 할 일을 구현하세요 | ||
* => MyPanel의 x좌표를 반복돌면서 증가시켜주고, 갱신된 값을 적용 | ||
* [2] 시작, 중지버튼 이벤트 처리 Anonymous class로 처리해보기 | ||
* 시작버튼 눌렀을 때 스레드를 동작시키세요 | ||
* */ | ||
public class MoveCircle extends JFrame implements Runnable { | ||
|
||
Container cp; | ||
JPanel rootP = new JPanel(); | ||
JButton btStart, btStop; | ||
MyPanel myP = new MyPanel(); | ||
Thread tr; | ||
private boolean flag; | ||
|
||
|
||
public MoveCircle() { | ||
super("::MoveCircle::"); | ||
cp = this.getContentPane(); | ||
cp.add(rootP, BorderLayout.NORTH); | ||
cp.add(myP, BorderLayout.CENTER); | ||
myP.setBackground(Color.white); | ||
|
||
rootP.setBackground(Color.white); | ||
|
||
rootP.add(btStart=new JButton("Start")); | ||
rootP.add(btStop= new JButton("Stop")); | ||
|
||
btStart.addActionListener(e->{ | ||
setTitle("$$$"); | ||
flag= true; | ||
tr=new Thread(this); | ||
tr.start(); | ||
}); | ||
|
||
btStop.addActionListener(e->{ | ||
setTitle("###"); | ||
flag= false; | ||
tr.interrupt(); | ||
}); | ||
|
||
/* | ||
btStart.addActionListener(new ActionListener() { | ||
public void actionPerformed(ActionEvent e) { | ||
setTitle("###"); | ||
flag = true; | ||
//Runnable 객체를 생성 => 스레드 생성할 때 인자로 넘기기 => 스레드 스타트 | ||
// MoveCircle r = new MoveCircle(); | ||
//이렇게 접근하면 안된다. 문법적으로는 맞으나 실제 메모리에 올라간 객체는 | ||
//다른 객체 | ||
// r.setSize(200,200); | ||
// r.setVisible(true); | ||
tr=new Thread(MoveCircle.this); | ||
tr.start(); | ||
} | ||
}); | ||
*/ | ||
/* | ||
btStop.addActionListener(new ActionListener() { | ||
public void actionPerformed(ActionEvent e) { | ||
setTitle("@@@"); | ||
flag=false; | ||
tr.interrupt(); | ||
//Runnable 객체를 생성 => 스레드 생성할 때 인자로 넘기기 => 스레드 스타트 | ||
} | ||
}); | ||
*/ | ||
|
||
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
}//------------------------- | ||
|
||
public void run() { | ||
while(flag) { | ||
if(myP.x>=myP.getWidth()) { | ||
myP.x=0; | ||
} | ||
//myP.x값을 증가 | ||
myP.x++; | ||
System.out.println(myP.x); | ||
//repaint()해서 적용 | ||
myP.repaint(); | ||
//sleep() | ||
try { | ||
Thread.sleep(10); | ||
}catch(InterruptedException e) { | ||
System.out.println(e); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
|
||
public static void main(String[] args) { | ||
MoveCircle my = new MoveCircle(); | ||
my.setBounds(1200, 100, 500, 500); | ||
//x, y, w, h | ||
my.setVisible(true); | ||
}//------------------------ | ||
|
||
}//class///////////////////////////////////// |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package day11; | ||
import javax.swing.*; | ||
import java.awt.*; | ||
import java.util.Random; | ||
public class MyPanel extends JPanel { | ||
|
||
int x=50, y=130, w=70, h=70; | ||
Color cr = Color.blue; | ||
|
||
public MyPanel() { | ||
//int r=(int)(Math.random()*256); | ||
Random ran = new Random(); | ||
int r = ran.nextInt(256)+0; //0~255사이의 랜덤한 정수를 발생시킨다 | ||
int g = ran.nextInt(256); | ||
int b = ran.nextInt(256); | ||
cr = new Color(r,g,b); | ||
} | ||
|
||
@Override | ||
public void paint(Graphics g) { | ||
super.paint(g); | ||
g.setColor(cr); | ||
g.fillOval(x, y, w, h); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package day11; | ||
// java.lang.Runnable 인터페이스를 상속받아 구현 => 추상메서드 : run() | ||
public class Snail implements Runnable | ||
{ | ||
@Override | ||
public void run() { | ||
for(int i=0; i<5; i++) { | ||
System.out.println("달팽이가 기어갑니다~~"); | ||
int sec=(int)(Math.random()*2000); | ||
try { | ||
Thread.sleep(sec); | ||
}catch(InterruptedException e) { | ||
System.out.println(e); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package day11; | ||
|
||
public class ThreadTest { | ||
|
||
public static void main(String[] args) { | ||
// 1. Runnable 객체를 생성 | ||
Snail r = new Snail(); // 코드를 가지고 있다 (run()) | ||
// 2. Thread객체 생성 | ||
Thread tr = new Thread(r); | ||
// 3. Start() 호출 | ||
tr.start(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package day11; | ||
//1만원씩 저축하는 스레드 | ||
public class UserIn extends Thread{ | ||
private Account account; | ||
public UserIn(String name, Account ac) { | ||
this.account=ac; | ||
setName(name); | ||
} | ||
|
||
public void run() { | ||
for(int i=0; i<5; i++) { | ||
account.save(1); // 1만원씩 저축 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package day11; | ||
//1~5만원 사이 금액을 랜덤하게 인출하는 스레드 | ||
import java.util.*; | ||
public class UserOut extends Thread{ | ||
private Account account; | ||
public UserOut(String name, Account ac) { | ||
this.account=ac; | ||
setName(name); | ||
} | ||
|
||
public void run() { | ||
Random ran = new Random(); | ||
for(int i=0; i<5; i++) { | ||
int val=ran.nextInt(5)+1; | ||
account.get(val); | ||
} | ||
} | ||
} |