十六、线程

写多线程的两种方法     (多线程)    之前写的程序都是单线程的(只有一条执行路径)


1.public class Helloworld1 {                          //第一种方法(实现Runnable接口)更好,能继承,又能实现尽量实现(实现了之后,还能继承)
 public static void main(String args[]) {
   Thread t1 = new Thread(new T("t1"));
   Thread t2 = new Thread(new T("t2"));
   t1.start();                                 //只有Thread里面的start()方法,才叫做开启线程
   t2.start();
  }
 }
 class T implements Runnable {                 //实现了Runnable接口的都是线程类
  String n;
  public T(String n) {
   this.n = n;
    }
     public void run() {        //方法的重写,重写接口Runnable里面的run()方法
      for(int i=1;i<=100;i++) {
       System.out.println(n+": "+i);
     }
   }
  }

2.public class Helloworld1 {                            //第二种方法(继承Thread类)
 public static void main(String args[]) {
   T t1 = new T("t1");
   T t2 = new T("t2");
   t1.start();
   t2.start();
  }
 }
 class T extends Thread {
  String n;
  public T(String n) {
   this.n = n;
    }
     public void run() {
      for(int i=1;i<=100;i++) {
       System.out.println(n+": "+i);
     }
   }
  }
....................................................................................................................................
线程的睡眠方法       (lang包底下Thread类里面的sleep()方法)

public class Helloworld1 {
 public static void main(String args[]) {
   T t1 = new T("t1");
   t1.start();
  }
 }
 class T extends Thread {
  String n;
  public T(String n) {
   this.n = n;
    }
     public void run() {
      for(int i=1;i<=100;i++) {
       try {if(i % 50 == 0) {            //sleep()方法会报编译期异常,要加上try-catch
        Thread.sleep(3000);       //sleep()方法可以加上睡眠时间,单位毫秒
       }}catch (Exception e) {
        }
       System.out.println(n+": "+i);
     }
   }
  }
....................................................................................................................................
暂停当前线程对象,并执行其他线程     (lang包底下Thread类里面的yield()方法)

public class Helloworld1 {
 public static void main(String args[]) {
   T t1 = new T("t1");
   T t2 = new T("t2");
   t1.start();
   t2.start();
  }
 }
 class T extends Thread {
  String n;
  public T(String n) {
   this.n = n;
    }
     public void run() {
      for(int i=1;i<=100;i++) {
       try {if(i % 10 == 0) {               //当t1(t2)到10的倍数时,执行t2(t1)
        Thread.yield();              //yield()方法和上面的sleep()方法一样,都是静态的方法
       }}catch (Exception e) {
        }
       System.out.println(n+": "+i);
     }
   }
  }
....................................................................................................................................
*(重点程序) 线程死锁(由于两边加上同步锁既只读锁,然后又去访问对方加锁的对象造成的)     (synchronized()只读锁)

public class Helloworld1 {
 public static void main(String args[]) {
  Thread t1 = new Thread(new T("t1",1));
  Thread t2 = new Thread(new T("t2",2));
  t1.start();
  t2.start();
  }
 }
 class T implements Runnable {
  static Object o1 = new Object();           //一定要加上static,要不然程序会再新建一个o1,o2
  static Object o2 = new Object();
  String n;
  int flag;
  public T(String n,int flag) {
   this.n = n;
   this.flag = flag;
   }
   public void run() {
    if(flag == 1) {
     synchronized(o1) {                              //synchronized关键字
      System.out.println("o1");
      synchronized(o2) {                        //在o1的只读锁还没解锁时,就对o2进行访问
       System.out.println("o2");
       }
      }
     }
     if(flag == 2) {
      synchronized(o2) {
       System.out.println("o2");
       synchronized(o1) {
        System.out.println("o1");
        }
       }
      }
    }
  }

posted @ 2012-07-17 23:44  建志  阅读(99)  评论(0)    收藏  举报