首页 > 编程语言 > 详细

java初学多线程

时间:2018-10-15 17:23:02      阅读:166      评论:0      收藏:0      [点我收藏+]

一、实现多线程的两种方式

  1.继承Tread类

  2.实现Runnable接口

  3.匿名内部类

二、具体实现

  1.继承Tread类

  

 1 public class Demo2_Thread {
 2     public static void main(String[] args) {
 3         MyThread mt = new MyThread();                //4,创建自定义类的对象
 4       mt.start();                                   //5,开启线程            
 5         for(int i = 0; i < 1000; i++) {
 6                         System.out.println("bb");
 7                     }
 8                 }
 9             
10             }
11 
12 
13  class MyThread extends Thread {                                    //1,定义类继承Thread
14         public void run() {                                            //2,重写run方法
15             for(int i = 0; i < 1000; i++) {                            //3,将要执行的代码,写在run方法中
16               System.out.println("aaaaaaaaaa");
17                     }
18                 }
19             }

2.实现Runnable接口

 

 1 public class Demo3_Runnable {
 2     /**
 3     * @param args
 4     */
 5  public static void main(String[] args) {
 6        MyRunnable mr = new MyRunnable();                        //4,创建自定义类对象
 7             //Runnable target = new MyRunnable();
 8         Thread t = new Thread(mr);                                //5,将其当作参数传递给Thread的构造函数
 9           t.start();                                                //6,开启线程
10                     
11                     for(int i = 0; i < 3000; i++) {
12                         System.out.println("bb");
13                     }
14                 }
15             }
16             
17             class MyRunnable implements Runnable {                            //1,自定义类实现Runnable接口
18                 @Override
19                 public void run() {                                            //2,重写run方法
20                     for(int i = 0; i < 3000; i++) {                            //3,将要执行的代码,写在run方法中
21                         System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaa");
22                     }
23                 }
24                 
25             }

3.匿名内部类实现两种方式

 

    new Thread() {                                                    //1,new 类(){}继承这个类
            public void run() {                                            //2,重写run方法
                for(int i = 0; i < 3000; i++) {                            //3,将要执行的代码,写在run方法中
                    System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaa");
                }
            }
        }.start();
new Thread(new Runnable(){                                        //1,new 接口(){}实现这个接口
            public void run() {                                            //2,重写run方法
                for(int i = 0; i < 3000; i++) {                            //3,将要执行的代码,写在run方法中
                    System.out.println("bb");
                }
            }
        }).start(); 

 三、多线程同步代码块问题’

java初学多线程

原文:https://www.cnblogs.com/lzq-java/p/9792184.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!