๐Ÿ’ป/JAVA

(36-37) [JAVA] 20-21์ผ

๋”ฐ๊ถˆ 2024. 4. 10. 16:05

4.4

package ch15.unit02;

import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class Ex01_tramsient {

	public static void main(String[] args) {
		String pathname = "demo2.txt";

		try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(pathname))) {

			oos.writeObject(new User("ํ™๊ธธ๋™", "010", 20));
			oos.writeObject(new User("์ด๊ธธ๋™", "010", 20));
			oos.writeObject(new User("๋ถ€๊ธธ๋™", "010", 20));

			System.out.println("ํŒŒ์ผ ์ €์žฅ ์™„๋ฃŒ~~!");

		} catch (Exception e) {
			e.printStackTrace();
		}

		try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(pathname))) {
			System.out.println("\n ํŒŒ์ผ ๋‚ด์šฉ...");

			while (true) {
				User vo = (User) ois.readObject();
				System.out.println(vo.getName() + ":" + vo.getTel() + ":" + vo.getAge());
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (EOFException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}

//์ง๋ ฌํ™” ๋Œ€์ƒ์—์„œ ์ œ์™ธ๋˜๋Š” ๊ฒƒ : ๋ฉ”์†Œ๋“œ,static ๋ณ€์ˆ˜,transient๋ณ€ํ›„ 
class User implements Serializable {
	private static final long serialVersionUID = 1L;

	private String name;
	private String tel;
	private int age;

	public User() {

	}

	public User(String name, String tel, int age) {
		this.name = name;
		this.tel = tel;
		this.age = age;

	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getTel() {
		return tel;
	}

	public void setTel(String tel) {
		this.tel = tel;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

}

 

 

package ch15.unit02;

public class Ex02_Runnable {

	public static void main(String[] args) {
		MyThread2 th = new MyThread2();
		Thread t = new Thread(th);
		t.start();

		try {
			for (int i = 1; i <= 10; i++) {
				System.out.println("main->" + i);
				Thread.sleep(100);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		System.out.println("main end...");

	}
}
	
/*
 * -์Šค๋ ˆ๋“œ ๊ตฌํ˜„ ๋ฐฉ๋ฒ•2
 * 1) Runnable ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌํ˜„ ํด๋ž˜์Šค ์ž‘์„ฑ
 * run() ๋ฉ”์†Œ๋“œ override
 * 2)Runnable ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌํ˜„ ํด๋ž˜์Šค ๊ฐ์ฒด ์ƒ์„ฑ
 * 3)Thread ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋ฉฐ, Thread ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์ƒ์„ฑ์ž์˜ ์ธ์ž์— 2)๋ฒˆ์—์„œ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. 
 * 
 */

	
class MyThread2 implements Runnable {
	
		public void run() {
			int n = 0;

			try {
				while (n < 20) {
					n++;
					System.out.println(Thread.currentThread().getName());
					Thread.sleep(500);

				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

 

package ch15.unit02;

public class Ex03_Runnable {

	public static void main(String[] args) {
		//์Šค๋ ˆ๋“œ ๊ตฌํ˜„ ๋ฐฉ๋ฒ• 3: ์ต๋ช… ํด๋ž˜์Šค 
		
		Thread t=new Thread(new Runnable() {
			public void run() {
				try {
					for(int i=1;i<=20;i++) {
						System.out.println("์Šค๋ ˆ๋“œ"+i);
		
					}
				} catch (Exception e) {
					// TODO: handle exception
				}
				
			}
		});
		
		t.start();
		
		try {
			for(int i=1;i<=10;i++) {
				System.out.println("๋ฉ”์ธ>"+i);
				Thread.sleep(100);
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("๋ฉ”์ธ๋˜!");

	}

}

 

 

package ch15.unit02;

public class Ex04_Runnable {

	public static void main(String[] args) {
		// ์Šค๋ ˆ๋“œ ๊ตฌํ˜„ ๋ฐฉ๋ฒ• 4 - ๋žŒ๋‹ค์‹
		
		Thread t=new Thread( () -> {
		
				try {
					for(int i=1;i<=20;i++) {
						System.out.println("์Šค๋ ˆ๋“œ"+i);
		
					}
				} catch (Exception e) {
					// TODO: handle exception
				}
	
		});
		
	t.start();
		
		try {
			for(int i=1;i<=10;i++) {
				System.out.println("๋ฉ”์ธ>"+i);
				Thread.sleep(100);
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("๋ฉ”์ธ๋˜!");
		
	

	}

}