多线程

来源:互联网 发布:华为软件开发云 编辑:程序博客网 时间:2024/05/16 13:42
多线程和多进程之间的区别:
本质区别:每个进程拥有自己的一整套变量,而线程则是共享数据。共享变量使线程之间的通信比
 进程更有效,容易。
Thread.sleep ----sleep是Thread的静态方法 sleep可以抛出InterruptedException 异常。
InterruptedException:
假如有两个线程,第一个线程正在运行,第二个没有运行,这时第二个线程启动运行并要求中断第一个线程,第一个线程就会出现InterruptedException异常并执行该异常下的语句。
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


/**
 * @version 2013-05-13
 * @author lucc
 * 展示一个小球的动态移动
 */
public class Bounce {
   public static void main(String args[]) {
  /*awt是单线程模式的,所有awt的组件只能在(推荐方式)事件处理线程中访问,
              从而保证组件状态的可确定性。
             把这个事件(new Runnable(设置计算器可见))添加到
    awt的事件处理线程当中去awt的事件处理线程会按照队列的顺序依次调用
              每个待处理的事件来运行 
  */
  EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
JFrame frame = new BounceFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
  
  });
   }
}
class BounceFrame extends JFrame {
public static final int DEFAULT_WIDHT =450;
public static final int DEFAULT_HEIGHT=350;
private static final int STEPS =1000;
private static final int DLAY =3;
private BallComponent comp;
//构造方法
public BounceFrame() {
setSize(DEFAULT_WIDHT,DEFAULT_HEIGHT);
setTitle("小球弹跳");

comp = new BallComponent();
add(comp,BorderLayout.CENTER);
JPanel buttonJpanel = new JPanel();
addButton(buttonJpanel,"start",new ActionListener(){


@Override
public void actionPerformed(ActionEvent e) {
addBall();

}

});
addButton(buttonJpanel,"close",new ActionListener(){


@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}

});
add(buttonJpanel,BorderLayout.SOUTH);
}
public void addButton (Container c,String title,ActionListener listener){
JButton button = new JButton(title);
c.add(button);
button.addActionListener(listener);

}
public void addBall(){
Ball ball =new Ball();
comp.add(ball);
for (int i=1;i<STEPS;i++) {
ball.move(comp.getBounds());
comp.paint(comp.getGraphics());
try {
Thread.sleep(DLAY);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}



}


import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;


/**
 * @author lucc
 * @version 2013-05-13
 *
 */
public class Ball {

       private double x =0;
       private double y =0;
       private double dx =1;
       private double dy =1;
  private static final int XSIZE = 15;
       private static final int YSIZE =15;
//Rectangle2D 类描述通过位置 (x, y) 和尺寸 (w x h) 定义的矩形。
    public void move (Rectangle2D bounds) {
      x+=dx;
      y+=dy;
      if(x<bounds.getMinX()) {
      x=bounds.getMinX();
      dx=-dx;
      }if(x+XSIZE>bounds.getMaxX()){
      x=bounds.getMaxX()-XSIZE;
      dx=-dx;
      }if(y<bounds.getMinY()) {
      y=bounds.getMinY();
      dy=-dy;
      }if(y+YSIZE>bounds.getMaxY()){
      y=bounds.getMaxY()-YSIZE;
      dy=-dy;
      }
    }
    public Ellipse2D getShape() {
    //Double 类以 double 精度定义椭圆
    //X,Y为左上角坐标,XSIZE,YSIZE 为椭圆的宽度和高度;
return  new Ellipse2D.Double(x,y,XSIZE,YSIZE);
   
    }
}
/**
 * @author lucc
 * @version 2013-05-13
 *
 */
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ArrayList;
import javax.swing.JPanel;




public class BallComponent  extends JPanel{
    private ArrayList<Ball> balls = new ArrayList<Ball>();
    
        public void add(Ball b) {
    balls.add(b);
    }
    public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D)g;
    for(Ball b:balls) {
    g2.fill(b.getShape());
    }
   
    }
}


MIN_PRIORITY MAX_PRIORITY 


yield()导致当前线程处于让步状态
t.SetDaemon(true) 将线程转换为守护线程
守护线程的唯一用途就是为其他线程提供服务。
  锁对象:有两种几只防止代码块受并发访问干扰
 synchronzied 关键字
ReentrantLock类
void lock() 获取这个锁,如果锁同时被另一个线程拥有则发生阻塞
void unlock 释放这个锁


一个锁对象可以有一个或者多个相关的条件对象,可以用newCondition方法获取一个条件对象

















原创粉丝点击