zoukankan      html  css  js  c++  java
  • Java多线程编程核心技术---多线程技能

    1、继承Thread

    /**
     * Copyright (C), 2018-2018,
     * FileName: MyThread
     * Author:   大象
     * Date:     2018-06-08 22:30
     */
    package com.daxiang.demo.thread;
    
    /**
     * 〈〉<br> 
     *
     * @author daxiang
     * @create 2018-06-08
     * @since 1.0.0
     */
    public class MyThread extends Thread{
        @Override
        public void run() {
            super.run();
            System.out.println("this is thread");
        }
    
        public static void main(String[] args) {
            MyThread myThread = new MyThread();
            myThread.start();
            System.out.println("线程结束");
        }
    }
    

    2、实现Runnable接口

    由于java规定只能单继承,所以为了满足多线程要求可以继承Runnable接口

    /**
     * Copyright (C), 2018-2018,
     * FileName: MyThreadByRunnable
     * Author:   大象
     * Date:     2018-06-08 22:33
     */
    package com.daxiang.demo.thread;
    
    /**
     * 〈〉<br> 
     *
     * @author daxiang
     * @create 2018-06-08
     * @since 1.0.0
     */
    public class MyThreadByRunnable implements Runnable{
    
        @Override
        public void run() {
            System.out.println("线程运行中");
        }
    
        public static void main(String[] args) {
            MyThreadByRunnable myThreadByRunnable = new MyThreadByRunnable();
            Thread thread = new Thread(myThreadByRunnable,"线程1");
            thread.start();
            System.out.println("运行结束");
        }
    }

    3、线程运行的随机性

    /**
     * Copyright (C), 2018-2018,
     * FileName: MyThreadByRandom
     * Author:   大象
     * Date:     2018-06-09 7:00
     */
    package com.daxiang.demo.thread;
    
    /**
     * 〈〉<br>
     *
     * @author daxiang
     * @create 2018-06-09
     * @since 1.0.0
     */
    public class MyThreadByRandom extends Thread {
    
        @Override
        public void run() {
            System.out.println("run thread:" + Thread.currentThread().getName());
            for (int j = 0; j < 10; j++) {
                int time = (int) Math.random() * 1000;
                try {
                    Thread.sleep(time);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("run:" + Thread.currentThread().getName());
            }
    
        }
    
        public static void main(String[] args) {
            MyThreadByRandom thread = new MyThreadByRandom();
            thread.setName("线程");
            thread.start();
            for (int i = 0; i < 10; i++) {
                int time = (int) Math.random() * 1000;
                try {
                    Thread.sleep(time);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("run:" + Thread.currentThread().getName());
            }
        }
    
    }
    

      

  • 相关阅读:
    linux中解压.tgz, .tar.gz ,zip ,gz, .tar文件
    hook(v16.7测试)?
    React优点?
    Overload和Override的区别。Overloaded的方法是否可以改变返回值的类型?
    说说数据库连接池工作原理和实现方案?
    short s1 = 1; s1 = s1 + 1;有什么错? short s1 = 1; s1 += 1;有什么错?
    Oracle的数据优化(经常被问到)?
    存储过程和函数的区别?
    Collection 和 Collections的区别?
    说说数据库连接池工作原理和实现方案?
  • 原文地址:https://www.cnblogs.com/daxiang2008/p/9157845.html
Copyright © 2011-2022 走看看