随笔-124  评论-49  文章-56  trackbacks-0

java.lang.Thread

【创建新线程的方法有两种】

1 定义线程类实现Runnable接口
public class MyThred implements Runnable{
     @overwrite
     public void run(){}
}

起用线程:
MyThred myThred=new MyThred();
Thread t=new Thread(myThred);
t.start();


2 定义线程类继承Thread类并重写其run方法
public class MyThred extends Thread{
     @overwrite
     public void run(){}
}

起用线程:
MyThred myThred=new MyThred();
myThred.start();

说明:一般使用接口来实现
-------------------------------------------------
【线程状态转换】

【线程控制基本方法】:
isAlive() 判断线程是否还“活”着
getPriority() 获得线程的优先级
setPriority() 设置线程的优先级
Thread.sleep() 将当前线程睡眠指定的毫秒数
join()  调用某线程的该方法,将当前线程与该线程“合并”,即等待
        该线程线束,再恢复当前线程的运行。
yield() 让出CPU,当前线程进入就绪队列等待调度
wait() 当胶线程进入对象的wait pool。
notify()/notifyAll() 唤醒对象的wait pool中的一个/所有等待线程。
Thread.currentThread() 得到当前线程
----------------------------------------------------------------
【sleep/join/yield 方法】

sleep示例:
public class TestInterrupt {
  public static void main(String[] args) {
    MyThread thread = new MyThread();
    thread.start();
    //主线程睡眠
    try {Thread.sleep(10000);}
    catch (InterruptedException e) {}
    //停下当前线程,这个方法一般不建议使用
    thread.interrupt();
    //使用这个结束线程
    shutDown();
  }
}

class MyThread extends Thread {
 boolean flag = true;
  public void run(){
    while(flag){
      System.out.println("==="+new Date()+"===");
      try {
 //当前子线程睡眠
        sleep(1000);
 //sleep时被打断会抛InterruptedException
      } catch (InterruptedException e) {
        return;
      }
    }
  }
  //结束线程的方法
  public void shutDown(){
      flag = false;
  }
}

jion示例:
public class TestJoin {
  public static void main(String[] args) {
    MyThread2 t1 = new MyThread2("abcde");
    t1.start();
    try {
        //当前线程合并到主线程,相当于方法调用
     t1.join();
    } catch (InterruptedException e) {}
     
    for(int i=1;i<=10;i++){
      System.out.println("i am main thread");
    }
  }
}
class MyThread2 extends Thread {
  //给当前线程起个别名
  MyThread2(String s){
   super(s);
  }
 
  public void run(){
    for(int i =1;i<=10;i++){
      System.out.println("i am "+getName());
      try {
       sleep(1000);
      } catch (InterruptedException e) {
       return;
      }
    }
  }
}

yield示例:
public class TestYield {
  public static void main(String[] args) {
    //可以用同一个线程类创建不同的线程对象,分别执行
    MyThread3 t1 = new MyThread3("t1");
    MyThread3 t2 = new MyThread3("t2");
    t1.start(); t2.start();
  }
}
class MyThread3 extends Thread {
  MyThread3(String s){super(s);}
  public void run(){
    for(int i =1;i<=100;i++){
      System.out.println(getName()+": "+i);
      if(i%10==0){
 //让出时间给其它线程执行
        yield();
      }
    }
  }
}

--------------------------------------------------------------
【线程的优先级别】getPriority()/setProority(int newPriority)

线程的优先级用数字表示,范围从1到10,默认为5.
Thread.MIN_PRIORITY=1
Thread.MAX_PRIORITY=10
Thread.NORM_PRIORITY=5

示例:
public class TestPriority {
 public static void main(String[] args) {
  Thread t1 = new Thread(new T1());
  Thread t2 = new Thread(new T2());
  //将t1的优先级提高3级
  t1.setPriority(Thread.NORM_PRIORITY + 3);
  t1.start();
  t2.start();
 }
}

class T1 implements Runnable {
 public void run() {
  for(int i=0; i<1000; i++) {
   System.out.println("T1: " + i);
  }
 }
}

class T2 implements Runnable {
 public void run() {
  for(int i=0; i<1000; i++) {
   System.out.println("------T2: " + i);
  }
 }
}

--------------------------------------------------------------------
【线程的同步】未看完

posted on 2009-11-29 21:30 junly 阅读(177) 评论(0)  编辑  收藏 所属分类: java

只有注册用户登录后才能发表评论。


网站导航: