Thread メモ
テスト時のサンプル。
import javax.swing.*;
import java.awt.event.*;
import java.awt.Container;
import java.util.Date;
public class JWW {
public static void main(String args[]){
MainFrame mainFrame = new MainFrame();
}
}
class MainFrame extends JFrame implements ActionListener {
_Thread tha = new _Thread("A");
_Thread thb = new _Thread("B");
_Thread thc = new _Thread("c");
Container contentpane;
JButton ctrl_button;
//JCanvas j_canvas;
public MainFrame(){
this.setTitle("SW");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(600,450);
this.setLocationRelativeTo(null);
contentpane = this.getContentPane();
ctrl_button = new JButton("Control");
contentpane.add(ctrl_button,java.awt.BorderLayout.SOUTH);
ctrl_button.addActionListener(this);
tha.setToT(false);tha.start();
thb.setToT(false);thb.start();
thc.start();
this.setVisible(true);
}
public void actionPerformed(ActionEvent event){
if (tha.getToT() == false) {
tha.setCnt(thb.getCnt());
tha.setToT(true); thb.setToT(false);
System.out.println("aftStartA: "+tha.getToT());
} else {
thb.setCnt(tha.getCnt());
tha.setToT(false); thb.setToT(true);
System.out.println("aftStartB: "+thb.getToT());
}
}
}
class _Thread extends Thread {
public _Thread(String name){ super(name);}
boolean tot = true;
int cnt=0;
public void run(){
while(true){
if (tot == true){
System.out.println(this.getName()+": "+(cnt++));
try {
sleep(500);
} catch (InterruptedException e){}
}
}
}
public void setToT(boolean tot){
this.tot=tot;
}
public boolean getToT(){
return this.tot;
}
public void setCnt(int cnt){
this.cnt=cnt;
}
public int getCnt(){
return cnt;
}
}
import javax.swing.*;
import java.awt.event.*;
import java.awt.Container;
import java.util.Date;
public class JWW {
public static void main(String args[]){
MainFrame mainFrame = new MainFrame();
}
}
class MainFrame extends JFrame implements ActionListener {
_Thread tha = new _Thread("A");
_Thread thb = new _Thread("B");
_Thread thc = new _Thread("c");
Container contentpane;
JButton ctrl_button;
//JCanvas j_canvas;
public MainFrame(){
this.setTitle("SW");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(600,450);
this.setLocationRelativeTo(null);
contentpane = this.getContentPane();
ctrl_button = new JButton("Control");
contentpane.add(ctrl_button,java.awt.BorderLayout.SOUTH);
ctrl_button.addActionListener(this);
tha.setToT(false);tha.start();
thb.setToT(false);thb.start();
thc.start();
this.setVisible(true);
}
public void actionPerformed(ActionEvent event){
if (tha.getToT() == false) {
tha.setCnt(thb.getCnt());
tha.setToT(true); thb.setToT(false);
System.out.println("aftStartA: "+tha.getToT());
} else {
thb.setCnt(tha.getCnt());
tha.setToT(false); thb.setToT(true);
System.out.println("aftStartB: "+thb.getToT());
}
}
}
class _Thread extends Thread {
public _Thread(String name){ super(name);}
boolean tot = true;
int cnt=0;
public void run(){
while(true){
if (tot == true){
System.out.println(this.getName()+": "+(cnt++));
try {
sleep(500);
} catch (InterruptedException e){}
}
}
}
public void setToT(boolean tot){
this.tot=tot;
}
public boolean getToT(){
return this.tot;
}
public void setCnt(int cnt){
this.cnt=cnt;
}
public int getCnt(){
return cnt;
}
}
コメント
コメントを投稿
「コメントを投稿できるユーザー」の範囲は変更される可能性があります。