20130713 线程总结
今天学到了另外一个工具——线程。
我觉得线程说得专业一点是线程,说的业余一点它就是一个能让多种方法同时运行的工具。
一般情况下,在同一时间只能运行一个方法。但不是有一个说法么,理想很丰满,现实很骨感。也许在我们看来,同一时间只能运行一个方法的问题解决起来很简单,让这些方法排好队,一个一个按顺序执行不就行了吗?但现实生活中并不是这样的,现实生活是高效率的,对程序的要求也一样。所以,要让不同的方法在同一时间同时运行起来,以提高效率。
就这些了。
以下是一个简单的线程程序:
package dyh20130713;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class drawBall extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* @param args
*/
private Graphics g;
public static void main(String[] args) {
//实例化对象
drawBall db=new drawBall();
//调用界面初始化方法
db.initGUI();
}
//定义界面初始化的方法
public void initGUI(){
this.setTitle("画移动小球");
this.setSize(new Dimension(500,500));
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(3);
JPanel conentJPane=(JPanel)this.getContentPane();
conentJPane.setOpaque(false);
this.setBackground(new Color(0,0,250));
//添加一个按钮
JButton jb=new JButton("开始");
this.add(jb);
this.setVisible(true);
//窗体可见后,在窗体上添加画布对象
g=this.getGraphics();
drawListener draw =new drawListener(g);
jb.addActionListener(draw);
}
}
package dyh20130713;
import java.awt.Color;
import java.awt.Graphics;
public class DrawThread extends Thread{
private int t=0;
//t=0时,小球从左上角弹出
//t=1时,小球从右上角弹出
//t=2时,小球从左下角弹出
//t=3时,小球从右下角弹出
private Graphics g;
public boolean go=true;
public DrawThread(Graphics g,int t){
this.t=t;
this.g=g;
}
//重写run的方法
public void run(){
if(t==0){
draw();
}else if(t==1){
draw1();
}else if(t==2){
draw2();
}else if(t==3){
draw3();
}
}
// public void changeGo() {
// go=!go;
// }
public void draw(){
for (int i=1;i<500;i++){
// if(!go){
g.clearRect(i-5, i-5, 60, 60);
g.setColor(new Color(0,0,0));
g.fillOval(i, i, 50, 50);
try{
Thread.sleep(100);
}catch(Exception ef){}
// }
// else{
// System.out.println("累了!休息一下!");
// try{
// Thread.sleep(1000);
// }catch(Exception ef){}
// }
}
}
public void draw1(){
for (int i=1;i<500;i++){
// if(!go){
g.clearRect(480-(i-5), 480-(i-5), 60, 60);
g.setColor(new Color(0,0,0));
g.fillOval(480-i,480-i, 50, 50);
try{
Thread.sleep(100);
}catch(Exception ef){}
// }
// else{
// System.out.println("累了!休息一下!");
// try{
// Thread.sleep(1000);
// }catch(Exception ef){}
// }
}
}
public void draw2(){
for (int i=1;i<500;i++){
// if(!go){
g.clearRect(i-5, 480-(i-5), 60, 60);
g.setColor(new Color(0,0,0));
g.fillOval(i,480-i, 50, 50);
try{
Thread.sleep(100);
}catch(Exception ef){}
// }
// else{
// System.out.println("累了!休息一下!");
// try{
// Thread.sleep(1000);
// }catch(Exception ef){}
// }
}
}
public void draw3(){
for (int i=1;i<500;i++){
if(go){
g.clearRect(480-(i-5), i-5, 60, 60);
g.setColor(new Color(0,0,0));
g.fillOval(480-i,i, 50, 50);
try{
Thread.sleep(100);
}catch(Exception ef){}
}
else{
System.out.println("累了!休息一下!");
try{
Thread.sleep(1000);
}catch(Exception ef){}
}
}
}
}
package dyh20130713;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class drawListener implements ActionListener {
private Graphics g;
private boolean go;
public drawListener(boolean go){
this.go=go;
}
public drawListener(Graphics g){
this.g=g;
}
public void actionPerformed(ActionEvent e) {
//创建线程对象,调用start方法使其运行
DrawThread thread =new DrawThread (g,0);
thread.start();
//thread .changeGo();
DrawThread thread1 =new DrawThread (g,1);
thread1.start();
//thread1.changeGo();
DrawThread thread2 =new DrawThread (g,2);
thread2.start();
//thread2.changeGo();
DrawThread thread3 =new DrawThread (g,3);
thread3.start();
//thread3.changeGo();
//控制对象的状态:控制线程对象的状态
//调用对象的方法
// thread .changeGo();
// thread1.changeGo();
// thread2.changeGo();
// thread3.changeGo();
}
}