자바

 

 

 

 

 

 

 

 

class NotThread 

package java16;

public class NotThread {

	static void print(){
		for(int i=1; i<=5; i++){
			System.out.println(i);
		}
	}
	
	public static void main(String[] args) {
		//single thread(작업단위가 1개, 순차적인 프로그램)
		System.out.println(Thread.currentThread().getName());
		//single thread(작업단위가 1개, 순차적인 프로그램)
		
		print();
		print();
	}
	
	
	
}

/*
출력 =>


main
1
2
3
4
5
1
2
3
4
5

*/

 

 

class MyThreadTest

package java16;

//멀티스레드를 구현하는 방법
//extends Thread
//implements Runnable

//single thread : 순차적인 진행
// multi thread : 병렬 진행
class MyThread extends Thread{
	
	@Override
	public void run() {
		//스레드 이름
			
		for(int i=10; i >0; i--){
			System.out.print(Thread.currentThread().getName() + " : ");
			System.out.println(i+" ");
			
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}	
	}	
}


public class MyThreadTest {

	public static void main(String[] args) {
		// 스레드 객체 생성(새로운 백그라운드 작업단위 추가)
		System.out.println(Thread.currentThread().getName());
		Thread t =new MyThread();
		t.setName("스레드1"); //스레드의 이름 설정
		//t.run();//새로 만든 스레드가 아닌 main 스레드 실행
		t.start(); //스레드 시작 요청
		//자바가상머신이 run() 를 자동으로 호출함
		
		Thread t2 =new MyThread();
		t2.setName("스레드2");
		t2.start();
		
		System.out.println("프로그램 종료.....");
	}
	
}

/*출력 =>
main
프로그램 종료.....
스레드1 : 10 
스레드2 : 10 
스레드2 : 스레드1 : 9 
9 
스레드2 : 스레드1 : 8 
8 
스레드2 : 스레드1 : 7 
7 
스레드1 : 6 
스레드2 : 6 
스레드2 : 스레드1 : 5 
5 
스레드2 : 스레드1 : 4 
4 
스레드1 : 3 
스레드2 : 3 

...

.
*/

 

 

class TestThread

package java16;

class MyRunnable implements Runnable{
	
	@Override
	public void run() {
		String name =Thread.currentThread().getName();
		for(int i=10; i >=0; i--){
			System.out.println(name + " : " + i);
		}
		
	}
}

public class TestThread {

	public static void main(String[] args) {
		//new Thread(Runnable을 구현한 객체)
		Thread t1 =new Thread(new MyRunnable());
		Thread t2 =new Thread(new MyRunnable());
		t1.setName("스레드1");//스레드의 이름 설정
		t2.setName("스레드2");
		t1.start(); //run() 이 호출됨
		t2.start();
	}
	
}
/*
출력 =>
스레드1 : 10
스레드1 : 9
스레드1 : 8
스레드1 : 7
스레드1 : 6
스레드1 : 5
스레드1 : 4
스레드1 : 3
스레드1 : 2
스레드1 : 1
스레드1 : 0
스레드2 : 10
스레드2 : 9
스레드2 : 8
스레드2 : 7
스레드2 : 6
스레드2 : 5
스레드2 : 4
스레드2 : 3
스레드2 : 2
스레드2 : 1
스레드2 : 0


*/

 

 

class ClockExam

 

package java16;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import java.awt.Font;
import java.util.Calendar;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ClockExam extends JFrame implements Runnable{

	private JPanel contentPane;
	Calendar cal ; //캘린더 객체
	String time ; // 라벨에 출력할 문자열
	private JLabel lblTime;

	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ClockExam frame = new ClockExam();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	
	
	public ClockExam() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 610, 452);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		lblTime = new JLabel("New label");
		lblTime.setForeground(Color.BLUE);
		lblTime.setFont(new Font("굴림", Font.PLAIN, 20));
		lblTime.setHorizontalAlignment(SwingConstants.CENTER);
		contentPane.add(lblTime, BorderLayout.CENTER);
		
		JButton btnClose = new JButton("종료");
		btnClose.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
		//System.exit(상태코드) 시스템을 강제로 종료
		// 0 정상종료,  -1 비정상 종료		
				System.exit(0);
			}
		});
		contentPane.add(btnClose, BorderLayout.SOUTH);
		
		Thread th =new Thread(this);
		th.start();
		
	}

	
	
	//현재 날짜, 시간을 구해서 라벨에 출력
	void putTiem(){
		
		cal =Calendar.getInstance();// 캘린더 객체 생성
//String.format("출력형식", 출력할 값들)
//%02d 2자리 숫자, 빈자리에는 0이 출력 됨		
		time =String.format("%4d-%02d-%02d %02d:%02d:%02d", 
				cal.get(Calendar.YEAR),
				cal.get(Calendar.MONTH)+1,
				cal.get(Calendar.DAY_OF_MONTH),
				cal.get(Calendar.HOUR_OF_DAY), 
				cal.get(Calendar.MINUTE),
				cal.get(Calendar.SECOND)
				);	
		
		//라벨 시간 출력
		lblTime.setText(time);
	}
	
	
	@Override
	public void run() {
		while(true){
			
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			putTiem();	
		}
	}
	
	
}




 

 

 

 

 

 

 

 

 

about author

PHRASE

Level 60  머나먼나라

당신을 좋게 말하지 말라. 그러면 당신은 신뢰할 수 없는 사람이 될 것이다. 또 당신을 나쁘게 말하지 말라. 그러면 당신은 당신이 말한 그대로 취급받을 것이다. -루소-

댓글 ( 4)

댓글 남기기

작성