`

线程实现及应用

阅读更多
   在java中,每一个程序至少有一个线程,就是主线程。而线程可以理解为程序内部一个独立的运行单位。线程的创建和实现主要有两种方式:
   一是继承Thread类实现线程。这种方法尽管是一种多线程实现方式,启动线程的唯一方法就是通过Thread类的start()实例方法。start()方法是一个native方法,它将启动一个新线程,并执行run()方法。这种方式实现多线程很简单,通过自己的类直接extend Thread,并复写run()方法,就可以启动新线程并执行自己定义的run()方法。例如:
public class MyThread extends Thread {
  public void run() {
   System.out.println("线程启动");
  }
}
在合适的地方启动线程如下:
MyThread myThread1 = new MyThread();
MyThread myThread2 = new MyThread();
myThread1.start();
myThread2.start();
   二是通过实现Runnable接口创建和实现线程。
    如果自己的类已经extends另一个类,就无法直接extends Thread,此时,必须实现一个Runnable接口,如下:
public class MyThread extends OneClass implements Runnable {
  public void run() {
   System.out.println("MyThread.run()");
  }
}
为了启动MyThread,需要首先实例化一个Thread,并传入自己的MyThread实例:
MyThread myThread = new MyThread();
Thread thread = new Thread(myThread);
thread.start();
事实上,当传入一个Runnable target参数给Thread后,Thread的run()方法就会调用target.run(),参考JDK源代码:
public void run() {
  if (target != null) {
   target.run();
  }
}

   在运用线程的示例中,写的是小球在窗体上的运动。
    下为小球运动的代码:
   //窗体对象类
    public class ThreadJFrame extends JFrame {


public static void main(String[] args){
ThreadJFrame jf = new ThreadJFrame();
jf.initUI();
}

public void initUI(){
this.setSize(400, 400);
this.setLayout(new FlowLayout());
JButton btn1 = new JButton("添加");
JButton btn2 = new JButton("开始");
JButton btn3 = new JButton("暂停");
this.add(btn1);
this.add(btn2);
this.add(btn3);
this.setVisible(true);
                 //监听器对象
ButtonActionListener l = new ButtonActionListener(this);
btn1.addActionListener(l);
btn2.addActionListener(l);
btn3.addActionListener(l);
}

}


                 //窗体监听器类
public class ButtonActionListener implements ActionListener {
ArrayList<DrawThread> list = new ArrayList<DrawThread>();
int count = 1;
JFrame jf;
public ButtonActionListener(JFrame jf){
this.jf=jf;
}

public void actionPerformed(ActionEvent e) {
if("添加".equals(e.getActionCommand())){
DrawThread thread = new DrawThread("Thread"+count++,jf);
thread.start();
list.add(thread);
}
else if("开始".equals(e.getActionCommand())){
for(int i = 0; i < list.size();  i++){
DrawThread thread = list.get(i);
thread.setRunFlag(true);
}

} else if("暂停".equals(e.getActionCommand())){
for(DrawThread thread : list){
thread.setRunFlag(false);
}
}

}

}


      // 线程对象类(用第一种方法)
public class DrawThread extends Thread {
private static final int LEFT = 1;
private static final int RIGHT = 2;
private static final int UP = 3;
private static final int DOWN = 4;
private String name;
private Graphics g;
private int x=0,y=0;
private int lastX=0,lastY=0;
private JFrame jf;
private int x_derection = RIGHT;
private int y_derection = DOWN;
private boolean runFlag = true;


public DrawThread(String name, JFrame jf){
this.name = name;
this.jf = jf;
}

public void setRunFlag(boolean flag){
this.runFlag = flag;
}

public void run() {
while(true){
if(!runFlag){
try {
sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
continue;
}
switch(x_derection) {
                             // x方向上小球的运动方式
case LEFT:
x -= 10;
break;
case RIGHT:
x += 10;
break;
}
switch(y_derection) {
                             // y方向上小球的运动方式
case UP:
y -= 10;
break;
case DOWN:
y += 10;
break;
}

if(x > jf.getWidth()-30){
x_derection = LEFT;
}
if(x <= 0)
x_derection = RIGHT;
if(y >= jf.getHeight()-30)
y_derection = UP;
if(y <= 30)
y_derection = DOWN;
g = jf.getGraphics();
g.clearRect(lastX, lastY, 30, 30);
g.setColor(Color.GREEN);
g.fillOval(x, y, 30, 30);
lastX = x;
lastY = y;
try {
sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}


这就是我对于线程的一些理解,可能还有我没有理解到的一些细节。语言的理解是一个过程,多接触就会越来越懂的,所以好好加油咯!!!
  • 大小: 12.5 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics