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(); } }
相关推荐
java多线程总结
C#.net同步异步SOCKET通讯和多线程总结2[参照].pdf
MFC 多线程总结, 开发为于windows的应用程序开发
JAVA多线程总结
这是我总结整理的Java线程总结,希望对大家有所帮助!
C#.net同步异步SOCKET通讯和多线程总结
C++多线程总结[归纳].pdf
C_同步异步SOCKET通讯和多线程总结.doc
Java线程总结.pdf
C# 多线程总结,比较详细和全面,包含多种方式的多线程方法的说明。
【完整课程列表】 1-java_IO(1)(共47页).ppt 1-java_IO(2)(共28页).ppt 2-oracle基础(1)(共48页).ppt 2-oracle基础(2)(共48页).ppt 3-oracle查询和SQL函数(共43页).ppt ...8-IO&线程总结(共7页).ppt
java多线程总结
C#dotnet同步异步SOCKET通讯和多线程总结
Java多线程总结.pdf
线程总结笔记 linux笔记。
C#.net同步异步SOCKET通讯和多线程总结 同步套接字通信 Socket支持下的网上点对点的通信 服务端实现监听连接,客户端实现发送连接请求,建立连接后进行发送和接收数据的功能 服务器端建立一个socket,设置好本机的ip...
不错的线程总结(全面上)1
详细的讲述了多线程的各种用法 Java线程:概念与原理 Java线程:创建与启动 Java线程:线程栈模型与线程的变量 Java线程:线程状态的转换 Java线程:线程的同步与锁 Java线程:线程的交互 ...Java线程:大总结