Java 初学者-java多线程02

今天继续学习了java与多线程操作有关的知识。

/*
 * 
 * 方法二:编写一个类实现java.lang.Runnable()接口
 * 
 */
public class ThreadTest{

    public static void main(String[] args) {
        // TODO Auto-generated method stub
      MyRunnable r=new MyRunnable();//创建一个可以运行的对象
      Thread t=new Thread(r);
      t.start();//启动线程
      for(int i=0;i<100;i++) {
          System.out.println("主线"+i);
      }
    }

}
//利用Thread中构造方法使其成为线程类
class MyRunnable implements Runnable{
public void run() {    
    for(int i=0;i<100;i++) {
        System.out.println("分支"+i);
    }
}
}
public class ThreadTest{

    public static void main(String[] args) {
        // TODO Auto-generated method stub
           Thread T=new Thread(new Runnable() {
               public void run() {
                   for(int i=0;i<100;i++) {
                       System.out.println("分支"+i);
                   }
               }
           });
           T.start();
           for(int i=0;i<100;i++) {
               System.out.println("主"+i);
           }
    }

}
View Code

 

明天计划学习java多线程。

posted @ 2020-08-21 21:19  好吗,好  阅读(69)  评论(0)    收藏  举报