Tread

来源:互联网 发布:爬虫软件的作用 编辑:程序博客网 时间:2024/04/28 15:07

package cric.thread;

import java.util.TimerTask;

public class Threads {
 MyThread myT;

 public void run() {
  // ///////创建线程///////
  MyThread mt = new MyThread();
  mt.setPriority(Thread.MAX_PRIORITY);
  mt.setName("my thread");
  // mt.setDaemon(true);
  mt.start();
  // ///////创建线程1///////
  MyThread1 mt1 = new MyThread1();
  // new Thread(mt1).setName("my thread1");
  new Thread(mt1).start();
  new Thread(mt1).start();
  new Thread(mt1).start();
  new Thread(mt1).start();
  new Thread(mt1).start();
  // ///////创建线程2///////
  MyThread2 mt2 = new MyThread2();
  // mt2.getThread().setName("my thread2");
  mt2.getThread().start();
  mt2.getThread().start();
  mt2.getThread().start();
  mt2.getThread().start();

  int i = 0;
  // while(true)
  {
   // if(i++==1000)break;
   System.out.println("main: " + Thread.currentThread().getName());
   // ////////最大与最小线程////////////
   // System.out.println(Thread.MAX_PRIORITY+"   "+Thread.MIN_PRIORITY);
  }
  mt.setPriority(Thread.MAX_PRIORITY);
  // System.out.println("当前线程: "+Thread.currentThread());
  // System.out.println("main: "+Thread.currentThread().getName());
 }

 public static void main(String[] args) {
  Threads t = new Threads();
   t.run();
  //volatile k;
 
  MyThread myT = new MyThread();
  //myT.start();
 }
 
 class Times extends TimerTask{
  @Override
  public void run() {
   //volatile int i;
  
  }
 }

}

class MyThread extends Thread {
 /*
  * public void run() { System.out.println("我的线程名字:"+getName());
  * System.out.println("我的线程属性:"+getPriority());
  * System.out.println("我的当前线程: "+currentThread());
  * System.out.println("我的当前线程名字: "+getName());
  * System.out.println("我的当前线程属性: "+getPriority()); }
  */
 public void run() {
  //while(true)
  {
   System.out.println("我的线程名字:" + getName());
   System.out.println(toString());
   yield();
  }
 }
}

class MyThread1 implements Runnable {
 int in = 0;
 long minPrime;

 MyThread1() {
 }

 MyThread1(long minprime) {
  minPrime = minprime;
 }

 public void run() {
  // while(true)
  {
   System.out.println("A我的线程名字:  in   "
     + Thread.currentThread().getName() + in++);
   System.out.println("A我的线程名字:" + toString());
   Thread t = new Thread();
   System.out.println("当前线程:        " + t.currentThread().toString());
  }
 }
}

class MyThread2 {
 int index = 0;

 class innerThread extends Thread {
  public void run() {
   // while(true)
   {
    System.out.println("B我的线程名字:index     " + getName() + index++);
    System.out.println(toString());
   }
  }
 }

 Thread getThread() {
  return new innerThread();
 }

}


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/jinwanmeng/archive/2010/10/23/5960744.aspx

原创粉丝点击