import javax.swing.JOptionPane;
class JavaPractice {
public static void main(String[] args) throws Exception {
String input = JOptionPane.showInputDialog("아무 값이나 입력하세요.");
System.out.println("입력한 값은 : " + input);
for (int i = 10; i > 0; i--) {
System.out.println(i);
try {
Thread.sleep(1000);
/* 단위 : ms / 1000ms = 1s */
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
import javax.swing.JOptionPane;
class MyThread extends Thread {
@Override
public void run() {
for (int i = 10; i > 0; i--) {
System.out.println(i);
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
class JavaPractice {
public static void main(String[] args) throws Exception {
Thread th = new MyThread();
th.start();
String input = JOptionPane.showInputDialog("아무 값이나 입력하세요.");
System.out.println("입력한 값은 : " + input);
}
}
Thread 클래스 >> sleep(long ms)
public static native void sleep(long millis) throws InterruptedException;
class StringPrint {
void display(String s) {
for (int i = 0; i < 5; i++) {
System.out.print(s);
}
System.out.println();
}
}
class PrintThread extends Thread {
private StringPrint sp = new StringPrint();
private String str;
public PrintThread(String str) {
this.str = str;
}
@Override
public void run() {
sp.display(str);
}
}
class JavaPractice {
public static void main(String[] args) throws Exception {
Thread th1 = new PrintThread("1");
Thread th2 = new PrintThread("2");
Thread th3 = new PrintThread("3");
Thread th4 = new PrintThread("4");
Thread th5 = new PrintThread("5");
th1.start();
th2.start();
th3.start();
th4.start();
th5.start();
}
}
[Console] (나오는 순서는 실행할 때마다 랜덤, 뒤죽박죽 나올때도 있다)
// StringPrint 클래스 >> display() >> for문 내용 변경
System.out.print(Thread.currentThread().getName() + ":");
System.out.print(s);
System.out.println();
[Console] (나오는 순서는 실행할 때마다 랜덤)
Thread-0:1
Thread-0:1
Thread-0:1
Thread-4:5
Thread-4:5
Thread-4:5
Thread-4:5
Thread-4:5
public static void main(String[] args) throws Exception {
System.out.println(Thread.currentThread().getName() + ":");
/* 후략 */
}
[Console]
main:
class StringPrint {
public synchronized void display(String s) {
for (int i = 0; i < 5; i++) {
System.out.print(s);
}
System.out.println();
}
}
class PrintThread extends Thread {
private StringPrint sp;
private String str;
public PrintThread(String str, StringPrint sp) {
this.str = str;
this.sp = sp;
}
@Override
public void run() {
sp.display(str);
}
}
class JavaPractice {
public static void main(String[] args) throws Exception {
StringPrint sp = new StringPrint();
Thread th1 = new PrintThread("1", sp);
Thread th2 = new PrintThread("2", sp);
Thread th3 = new PrintThread("3", sp);
Thread th4 = new PrintThread("4", sp);
Thread th5 = new PrintThread("5", sp);
th1.start();
th2.start();
th3.start();
th4.start();
th5.start();
}
}
[Console] (나오는 순서는 실행할 때마다 랜덤, 뒤죽박죽 없이 출력한다 - 동기화 적용)
11111
33333
55555
44444
22222
InetAdrress : IP 주소를 다루는 것을 수행한다.
(위치 : java.net.InetAddress.class)
Socket : 서버와 소통하기 위해 데이터 통/수신을 한다. 소켓은 클라이언트 소켓을 다룬다.
(위치 : java.net.Socket.class)
SeverSocket : 클라이언트와 소통하기 위해 서버 측에서 설정하는 소켓.
((위치 : java.net.ServerSocket.class))
import java.net.InetAddress;
import java.util.Scanner;
class InetAdressEx {
Scanner scan;
public InetAdressEx() {
System.out.println("Host 이름을 입력하세요");
scan = new Scanner(System.in);
try {
InetAddress inetAddress = InetAddress.getByName(scan.next());
System.out.println("Computer Name : " + inetAddress.getHostName());
System.out.println("Computer IP : " + inetAddress.getHostAddress());
} catch (Exception e) {
e.getMessage();
}
}
}
public class Hello {
public static void main(String[] args) {
new InetAdressEx();
}
}
[Console]
Host 이름을 입력하세요
(입력 >>) www.naver.com
Computer Name : www.naver.com
Computer IP : 223.130.200.107
class Hello {
public static void main(String args[]) {
Runnable r = new RunnableEx12();
new Thread(r).start();
new Thread(r).start();
}
}
class Account {
private int balance = 1000;
public int getBalance() {
return balance;
}
public void withdraw(int money) {
if (balance >= money) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
balance -= money;
}
} // withdraw
}
class RunnableEx12 implements Runnable {
Account acc = new Account();
public void run() {
while (acc.getBalance() > 0) {
// 100, 200, 300중의 한 값을 임으로 선택해서 출금(withdraw)
int money = (int) (Math.random() * 3 + 1) * 100;
acc.withdraw(money);
System.out.println("balance:" + acc.getBalance());
}
} // run()
}
[Console]
balance:700
balance:700
balance:600
balance:300
balance:-100
balance:-100
(계좌 잔액은 음의 값이 나올 수 없는데, 어느새 잔고가 -100이 되어버렸다.)
class Hello {
public static void main(String args[]) {
Runnable r = new RunnableEx12();
new Thread(r).start();
new Thread(r).start();
}
}
class Account {
private int balance = 1000;
public int getBalance() {
return balance;
}
public synchronized void withdraw(int money) {
if (balance >= money) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
balance -= money;
}
}
}
class RunnableEx12 implements Runnable {
Account acc = new Account();
public void run() {
while (acc.getBalance() > 0) {
int money = (int) (Math.random() * 3 + 1) * 100;
acc.withdraw(money);
System.out.println("balance:" + acc.getBalance());
}
}
}
[Console]
balance:700
balance:500
balance:400
balance:100
balance:100
balance:100
balance:100
balance:0
balance:0