多线程

来源:互联网 发布:光学识别软件 编辑:程序博客网 时间:2024/05/15 03:46

线程的基本概念
简单的说:线程就是一个程序里不同的执行路径,在同一个时间点上cpu只会有一个线程在执行,Java里的多线程是通过java.lang.Thread类来实现的,每个线程都拥有自己独立的方法栈空间。

java线程的创建和启动
第一种
定义线程类实现Runnable接口:
Thread myThread = new Thread(target) //target为Runnable接口类型
Runnable中只有一个方法:
public void run();用以定义线程运行体
第二种
可以定义一个Thread的子类并重写其run方法:
clas MyThread extends Thread{
public void run(){}
}
线程类必须通过执行Thread的start()方法启动一个新的线程,如果调用run()方法是属于方法的调用,不会启动一个新的线程,推荐使用第一种方式创建线程,使用接口较为灵活。

第三种

可以使用Timer和TimerTask组合

在这种方式中,Timer是类似闹钟的功能,也就是定时,或每隔一时间隔触发一次线程。其实Timer本身就是一种线程,只是这个线程是用来实现调用其他线程。而TimerTask是一个抽象类,该类实现Runnable接口,所以具备多线程的能力。

下面就让我们看看这三种方法来实现字体滚动的代码:

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class TestThread extends Frame  {

 private static final long serialVersionUID = -6305635168612485786L;
    Label lbl=new Label("线程的概念比较复杂,但在java中是比较简单的");
    public TestThread(){
     super("多线程");
      this.add(lbl);
      this.setSize(400, 420);
   this.setLocation(450, 150);
   this.setVisible(true); 
   new TestThread1(lbl).start();
     this.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
     System.exit(0);
    }
   });
    }  
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  new TestThread();
 }
 class TestThread1 extends Thread{
  Label lbl=null;
  public TestThread1(Label _lbl) {
   lbl = _lbl;
  }
  public void run() {
   String str = lbl.getText();
   while (true) {
    try {
     str = str.substring(1) + str.substring(0, 1);
     lbl.setText(str);
     Thread.sleep(200);
    } catch (InterruptedException ie) {
     ie.printStackTrace();
    }
   }
  } 
 }
 }

 

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class TestThread1 extends Frame  {

 private static final long serialVersionUID = -6305635168612485786L;
    Label lbl=new Label("线程的概念比较复杂,但在java中是比较简单的");
    public TestThread1(){
     super("多线程");
      this.add(lbl);
      this.setSize(400, 420);
   this.setLocation(450, 150);
   this.setVisible(true); 
   new TestThread2(lbl).start();
     this.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
     System.exit(0);
    }
   });
    }  
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  new TestThread();
 }
 class TestThread2 implements Runnable {

  Label lb = null;

  public TestThread2(Label _lb) {
   lb = _lb;
  }

  public void start() {
   // TODO Auto-generated method stub  
  }

  public void run() {
   String str = lb.getText();
   while (true) {
    try {
     str = str.substring(1) + str.substring(0, 1);
     lb.setText(str);
     Thread.sleep(200);
    } catch (InterruptedException ie) {
     ie.printStackTrace();
    }
   }
  }
 }
 }

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.TimerTask;

public class TestThread2 extends Frame  {

 private static final long serialVersionUID = -6305635168612485786L;
    Label lbl=new Label("线程的概念比较复杂,但在java中是比较简单的");
    public TestThread2(){
     super("多线程");
      this.add(lbl);
      this.setSize(400, 420);
   this.setLocation(450, 150);
   this.setVisible(true); 
   new TestThread3(lbl).start();
     this.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
     System.exit(0);
    }
   });
    }  
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  new TestThread();
 }

 class TestThread3 extends TimerTask {
  Label lb = null;

  public TestThread3(Label _lb) {
   lb = _lb;
  }

  public void start() {
   // TODO Auto-generated method stub
   
  }

  public void run() {
   String str = lb.getText();
   try {
    str = str.substring(1) + str.substring(0, 1);
    lb.setText(str);
    Thread.sleep(200);
   } catch (InterruptedException ie) {
    ie.printStackTrace();
   }
  }
 }
}