java 多线程1

来源:互联网 发布:超出数组界限 编辑:程序博客网 时间:2024/05/18 01:50

/*//一个关于单线程休眠的实例
public class AA {
 public static void main(String[] args) {
  Thread t = Thread.currentThread();//创建Thread类的实例对象
  t.setName("单线程");
  System.out.println(t.getName()+" is a running thread");
  try {
   for(int i=0; i<5; i++){
    System.out.println("sleep time "+i);
    Thread.sleep(300);//让当前线程休眠300ms后再运行
   }
  } catch (InterruptedException e) {
   System.out.println("thread is error");
  }
 }

}*/
/*什么是线程以及线程的基本状态
 * 线程指在程序执行过程中,能够执行程序代码的一个执行单位,每个程序至少有一个线程,那就是程序本身
 * Java中线程有五种基本状态:创建、就绪、运行、阻塞、终止
 * 进入阻塞状态的可能原因:
 * 1.调用了该线程的sleep()休眠方法
 * 2.调用了该线程的wait()休眠方法
 * 3。 调用了该线程的suspend()休眠方法
 * 4。 该线程正在等待I/O流操作完成*/
//多线程的创建方式有两种:1、 继承Thread类创建线程2、 实现Runnable接口创建线程
/*实现Runnable接口除了拥有和继承Thread类一样的功能外,还具有以下功能
 * 1、 适合多个相同程序代码的线程处理同一资源的情况,可以把线程同程序中的数据有效分离,较好的体现了面向对象的思想
 * 2、 可以避免由于Java得单继承特性带来的局限。例如,class Student 已经继承了class Person类,如果要把Student类
 * 放入多线程中去,那么就不能使用Thread类的方式。因为Java不支持多继承。所以就只有使用实现Runnable接口的方式了
 * 3、 增强了代码的健壮性,代码能够被多个线程共同访问,代码与数据时独立的。多个线程可以操作相同的数据,与他们的
 * 代码无关。当线程被构造时,需要的代码和数据通过一个对象作为构造函数实参传递进去,这个对象就是一个实现了
 * Runnable接口累的实例*/
//应用继承Thread类的方法实现多线程的例子
/*class ThreadSubName extends Thread{
 private String threadName;
 private int Ms;
 public ThreadSubName(String name, int ms){
  this.threadName = name;
  this.Ms = ms;
 }
 public void run(){
  try {
   sleep(Ms);
  } catch (InterruptedException e) {
   // TODO Auto-generated catch block
   System.out.println("The Thread is error");
  }
  System.out.println("the name is "+threadName+" start sleep "+Ms+" ms");
 }

public class AA{
 public static void main(String[] args){
  ThreadSubName t1 = new ThreadSubName("Thread 1",200);
  ThreadSubName t2 = new ThreadSubName("Thread 2", 100);
  ThreadSubName t3 =  new ThreadSubName("Thread 3", 300);
  t1.start();
  t2.start();
  t3.start();
 }
}*/ 
 //运行结果如下:
/*the name is Thread 2 start sleep 100 ms
the name is Thread 1 start sleep 200 ms
the name is Thread 3 start sleep 300 ms
注意,从结果我们可以看出多线程并不是顺着程序的流程执行的。主要原因在sleep(Ms)这条语句。
*/

//应用Runnable接口的方法创建多线程
class RunnableDemo extends ThreadRun implements Runnable{
 Thread t2 = null;
 public void RDemo(RunnableDemo r1){
  Thread t1 = new Thread(r1,"first Thread");
  System.out.println("正在运行的是" + t1);
  t2 = new Thread(r1,"second thread");
  System.out.println("创建第二个线程");
  System.out.println("第一个线程开始休眠");
  t2.start();//启动t2线程
  try {
   t1.sleep(400);//t1休眠400ms
  } catch (InterruptedException e) {
   // TODO Auto-generated catch block
   System.out.println("第一个线程错误");
  }
  System.out.println("第一个线程恢复运行");
  
 }
 public void run(){
  for(int i=0;i<800;i+=100){
   System.out.println("第二个线程的休眠时间  "+i);
   try {
    t2.sleep(i);
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    System.out.println("第二个线程错误");
   }
   
  }
  System.out.println("第二个线程结束");
 }
}
class ThreadRun{
 public String print(){  
  return "我是Runnable的父类";
 }
}
public class AA{
 public static void main(String[] args){
  //注意r1不是线程,只是个普通的类对象。创建真正的线程对象时,必须使用带有Runnable参数的Thread类创建对象
  //例如,Thread t1 = new Thread(r1,"first Thread");t1就是线程对象,在Thread类中带有Runnable接口的构造方法有
  //public Thread(Runnable target) public Thread(ThreadGroup group,Runnable target)
  //public Thread(Runnable target, String name) public Thread(ThreadGroup group, Runnable target, String name)
  RunnableDemo r1 = new RunnableDemo();
  r1.RDemo(r1);
  System.out.println(r1.print());
 }
}
/*程序的运行结果如下:
 * 正在运行的是Thread[first Thread,5,main]
创建第二个线程
第一个线程开始休眠
第二个线程的休眠时间  0
第二个线程的休眠时间  100
第二个线程的休眠时间  200
第二个线程的休眠时间  300
第一个线程恢复运行
我是Runnable的父类
第二个线程的休眠时间  400
第二个线程的休眠时间  500
第二个线程的休眠时间  600
第二个线程的休眠时间  700
第二个线程结束
 * */