java之多线程(Thread)

时间:2022-05-05
本文章向大家介绍java之多线程(Thread),主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
 1 package DEMO;
 2 //主线程
 3 public class Example12_2 {
 4   public static void main(String [] args )
 5   {      
 6      Thread mydad ;   //用Thread声明线程
 7      Thread mymom ; 
 8         baba ba  ;       //ba是目标对象
 9      mom  ma  ;  
10      ba = new baba();   //创建目标对象
11      ma = new mom();   
12      mydad = new Thread(ba);   //创建线程,其目标对象是bab
13      mymom = new Thread(ma);   //创建线程 ,其目标对象是ma 
14      mydad.start();   //启动线程
15      mymom.start(); 
16 //主线程  
17   for(int i=1 ;i<=20 ; i++)
18      System.out.print("me"+i+" ");
19 
20  }
21 }
22 
23 class  baba implements Runnable     //实现Runnable
24 {
25   public void run()
26   {
27     for(int i=1;i<=20;i++)
28        System.out.print(" dad"+i+" ");  
29   }
30 }
31 
32 class mom implements Runnable     //实现Runnable
33 {
34     public void run()
35     {
36       for(int i=1; i<=20 ;i++)
37           System.out.print("Mon"+i+" ");
38     }
39 }