class Main{
public static void main(String[] args) {
myTimer th = new myTimer();
myTimer th1 = new myTimer();
th.start();
th1.start();
}
}
import javax.swing.*;
import java.awt.*;
class TimerThread extends Thread{
private JLabel timerLabel1;
public TimerThread(JLabel timerLabel) {
this.timerLabel1=timerLabel;
}
public void run() {
int n=0;
while(true) {
timerLabel1.setText(Integer.toString(n));
n++;
try {
Thread.sleep(10000);
}
catch(InterruptedException e) {
return;
}
}
}
}
public class Main extends JFrame{
public Main() {
setTitle("Thera 상속");
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container c=getContentPane();
c.setLayout(new FlowLayout());
JLabel timerLabel=new JLabel();
timerLabel.setFont(new Font("Gothic",Font.ITALIC,80));
c.add(timerLabel);
TimerThread th=new TimerThread(timerLabel);
setSize(300,170);
setVisible(true);
th.start();
}
public static void main(String[] args) {
new Main();
}
}
import javax.swing.*;
import java.awt.*;
class TimerRunnable implements Runnable{
private JLabel timerLabel2;
public TimerRunnable(JLabel timerLabel) {
this.timerLabel2=timerLabel;
}
public void run() {
int n=0;
while(true) {
timerLabel2.setText(Integer.toString(n));
n++;
try {
Thread.sleep(1000);
}
catch(InterruptedException e) {
return;
}
}
}
}
public class Main extends JFrame{
public Main() {
setTitle("문제");
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container c=getContentPane();
c.setLayout(new FlowLayout());
JLabel timerLabel1=new JLabel();
timerLabel1.setFont(new Font("Gothic",Font.ITALIC,80));
c.add(timerLabel1);
TimerRunnable runnable=new TimerRunnable(timerLabel1);
Thread th=new Thread(runnable);
setSize(250,150);
setVisible(true);
th.start();
}
public static void main(String[] args) {
new Main();
}
}
import java.awt.*;
import javax.swing.*;
class FlickeringLabel extends JLabel implements Runnable{
private long delay;
public FlickeringLabel(String text,long delay) {
super(text);
this.delay=delay;
setOpaque(true);
Thread th=new Thread(this);
th.start();
}
public void run() {
int n=0;
while(true) {
if(n==0)
setBackground(Color.YELLOW);
else
setBackground(Color.GREEN);
if(n==0) n=1;
else n=0;
try {
Thread.sleep(delay);
}
catch(InterruptedException e) {
return;
}
}
}
}
public class Main{
public static void main(String[] args) {
long id=Thread.currentThread().getId();
String name=Thread.currentThread().getName();
int priority=Thread.currentThread().getPriority();
Thread.State s = Thread.currentThread().getState();
System.out.println(name);
System.out.println(id);
System.out.println(priority);
System.out.println(s);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class TimerRunnable implements Runnable{
private JLabel timJLabel;
public TimerRunnable(JLabel timerLabel) {
this.timJLabel=timerLabel;
}
public void run() {
int n=0;
while(true) {
timJLabel.setText(Integer.toString(n));
n++;
try {
Thread.sleep(100);
}
catch (InterruptedException e) {
// TODO: handle exception
return ;
}
}
}
}
public class Main extends JFrame{
private Thread th;
public Main() {
setTitle("예제");
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container c=getContentPane();
c.setLayout(new FlowLayout());
JLabel timerLabel=new JLabel();
timerLabel.setFont(new Font("Gothic",Font.ITALIC,80));
TimerRunnable runnable=new TimerRunnable(timerLabel);
th=new Thread(runnable);
c.add(timerLabel);
JButton btn=new JButton("kill Timer");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
th.interrupt();
JButton btn=(JButton)e.getSource();
btn.setEnabled(false);
}
});
c.add(btn);
setSize(300,170);
setVisible(true);
th.start();
}
public static void main(String[] args) {
new Main();
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class randomThread extends Thread{
private Container contentPane;
private boolean flag=false;
public randomThread(Container contentPane) {
this.contentPane=contentPane;
}
public void finish() {
flag=true;
}
public void run() {
while(true) {
int x=((int)(Math.random()*contentPane.getWidth()));
int y=((int)(Math.random()*contentPane.getHeight()));
JLabel label=new JLabel("java");
label.setSize(80, 30);
label.setLocation(x, y);
contentPane.add(label);
contentPane.repaint();
try {
Thread.sleep(300);
if(flag==true) {
contentPane.removeAll();
label=new JLabel("finish");
label.setSize(80, 30);
label.setLocation(100, 100);
label.setForeground(Color.RED);
contentPane.add(label);
contentPane.repaint();
return;
}
}
catch (InterruptedException e) {
// TODO: handle exception
return;
}
}
}
}
public class Main extends JFrame{
private randomThread th;
public Main() {
setTitle("예제");
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container c=getContentPane();
c.setLayout(null);
c.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
th.finish();
}
});
setSize(300, 200);
setVisible(true);
th=new randomThread(c);
th.start();
}
public static void main(String[] args) {
new Main();
}
}
*/
public class Main{
public static void main(String[] args) {
ShareBoard board=new ShareBoard();
Thread th1=new StudentThread("kitae", board);
Thread th2=new StudentThread("hyosoo", board);
th1.start();
th2.start();
}
}
class ShareBoard{
private int sum=0;
synchronized public void add() {
int n=sum;
Thread.yield();
n+=10;
sum=n;
System.out.println(Thread.currentThread().getName()+":"+sum);
}
public int getSum() {return sum;}
}
class StudentThread extends Thread{
private ShareBoard board;
public StudentThread(String name,ShareBoard board) {
super(name);
this.board=board;
}
public void run() {
for(int i=0;i<10;i++)
board.add();
}
}