zoukankan      html  css  js  c++  java
  • 线程基础

    public static void main(String[] args) throws InterruptedException {
            System.out.println(Thread.currentThread().getName() + "正在执行");
            test();
            System.out.println(Thread.currentThread().getName() + "执行完毕");
        }
    
        public static void test() throws InterruptedException {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName() + "子线程正在执行");
                    try {
                        Thread.currentThread().sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "子线程执行完毕");
                }
            }).start();
            Thread.currentThread().sleep(4000);
            System.out.println(Thread.currentThread().getName() + "执行test方法完毕");
        }

     执行结果:

    main正在执行
    Thread-0子线程正在执行
    Thread-0子线程执行完毕
    main执行test方法完毕
    main执行完毕
    View Code
    package com.thread.demo;
    /**
     * 线程:通常用于解决同时运行多个任务
     * "同时"是感官上,实际上多个线程之间是走走停停的
     * 并发运行方式。
     * 
     * 创建线程有两种方式:
     * 第一种,直接继承Thread类并重写run方法来
     * 定义线程要执行的任务代码
     * @author Administrator
     *
     */
    public class ThreadDemo1 {
        public static void main(String[] args) {
            Thread t1 = new MyThread1();
            Thread t2 = new MyThread2();
            /*
             * 启动线程要执行start方法。使其被纳入到
             * 线程调度中。一旦线程调度将CPU时间片段
             * 分配给当前线程,那么当前线程的run方法
             * 就自动被调用开始执行。
             * start执行的过程中run方法是不会被调用的,
             * 但是执行完毕后run方法会很快的被执行起来。
             */
            t1.start();
            t2.start();
        }
    }
    /**
     * 第一种创建线程的方式有两个不足:
     * 1:由于java是单继承,所以当前类若继承了Thread类
     *   后就不能再继承其他类,这对于系统架构设计来讲很
     *   不理想。
     * 2:线程内部重写run方法来定义任务导致了线程与任务
     *   有一个强耦合关系,线程会变得重用性,灵活度非常
     *   差  
     * 优点:
     * 定义简单,所以常被用作匿名内部类的创建方式。
     */
    class MyThread1 extends Thread{
        public void run(){
            for(int i=0;i<1000;i++){
                System.out.println("你是谁啊?");
            }
        }
    }
    class MyThread2 extends Thread{
        public void run(){
            for(int i=0;i<1000;i++){
                System.out.println("我是查水表的!");
            }
        }
    }
    package com.thread.demo;
    /**
     * 第二种创建线程的方式
     * 实现Runnable接口来单独定义线程要执行的任务
     * @author Administrator
     *
     */
    public class ThreadDemo2 {
        public static void main(String[] args) {
            Runnable r1 = new MyRunnable1();
            Runnable r2 = new MyRunnable2();    
            Thread t1 = new Thread(r1);
            Thread t2 = new Thread(r2);    
            t1.start();
            t2.start();
        }
    }
    class MyRunnable1 implements Runnable{
        public void run() {
            for(int i=0;i<1000;i++){
                System.out.println("你是谁啊?");
            }
            
        }
    }
    class MyRunnable2 implements Runnable{
        public void run() {
            for(int i=0;i<1000;i++){
                System.out.println("我是查水表的!");
            }
            
        }
    }
    package com.thread.demo;
    /**
     * 用匿名内部类的形式使用方式1和方式2创建Thread
     * @author Administrator
     *
     */
    public class ThreadDemo3 {
        public static void main(String[] args) {
            //方式1:直接继承Thread
            Thread t1 = new Thread(){
                public void run(){
                    for(int i=0;i<200;i++){
                        System.out.println("你是谁啊?");
                    }
                }
            };
            //方式2:创建Runnable
            Runnable runn = new Runnable(){
                public void run(){
                    for(int i=0;i<200;i++){
                        System.out.println("我是查水表的!");
                    }
                }
            };
            Thread t2 = new Thread(runn);
            
            t1.start();
            t2.start();
            
            System.out.println("程序结束了!");
        }
    }
    package com.thread.demo;
    /**
     * 获取线程信息的相关API
     * @author Administrator
     *
     */
    public class ThreadDemo5 {
        public static void main(String[] args) {
            Thread t = Thread.currentThread();
            
            long id = t.getId();
            System.out.println("id:"+id);
            
            String name = t.getName();
            System.out.println("name:"+name);
            //线程优先级
            int priority = t.getPriority();
            System.out.println("priority:"+priority);
            
            System.out.println("isAlive:"+t.isAlive());
            System.out.println("isDaemon:"+t.isDaemon());
            System.out.println("isInterrupted:"+t.isInterrupted());
            
        }
    }
    package com.thread.demo;
    /**
     * 线程优先级
     * 
     * 线程调度的工作不可控,对于线程而言:
     * 1:时间片长短不可控
     * 2:时间片分配给哪个线程不可控
     * 
     * 线程只能被动被分配时间片后并发运行,不能
     * 主动要求获取时间片。
     * 
     * 设置线程优先级可以最大程度的干预线程调度
     * 工作
     * 理论上,线程优先级高的线程分配时间片的次数多。
     * 
     * @author Administrator
     *
     */
    public class ThreadDemo6 {
        public static void main(String[] args) {
            Thread min = new Thread(){
                public void run(){
                    for(int i=0;i<10000;i++){
                        System.out.println("min");
                    }
                }
            };
            Thread max = new Thread(){
                public void run(){
                    for(int i=0;i<10000;i++){
                        System.out.println("max");
                    }
                }
            };
            Thread norm = new Thread(){
                public void run(){
                    for(int i=0;i<10000;i++){
                        System.out.println("norm");
                    }
                }
            };        
            min.setPriority(Thread.MIN_PRIORITY);
            max.setPriority(Thread.MAX_PRIORITY);        
            min.start();
            norm.start();
            max.start();
            
        }
    }
    package com.thread.demo;
    /**
     * 守护线程
     * 又名后台线程。
     * 默认创建出来的线程都是前台线程。后台线程是通过
     * 方法指定,使前台线程变为后台线程。
     * 
     * 后台线程与前台线程的区别主要体现在结束时机:
     * 当一个进程中的所有前台线程全部结束时,进程结束,无论
     * 进程中的后台线程是否还在运行,都要强制中断。
     * @author Administrator
     *
     */
    public class ThreadDemo9 {
        public static void main(String[] args) {
            /*
             * rose的扮演者:前台线程
             */
            Thread rose = new Thread(){
                public void run(){
                    for(int i=0;i<10;i++){
                        System.out.println(
                            "rose:let me go!"
                        );
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                        }
                    }
                    System.out.println(
                        "rose:啊啊啊啊啊AAAAaaaa.....");
                    System.out.println("效果:噗通!");
                }
            };
            
            Thread jack = new Thread(){
                public void run(){
                    while(true){
                        System.out.println(
                            "jack:you jump!i jump!");
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                        }
                    }
                }
            };
            //设置为后台线程。要在start方法之前调用
            jack.setDaemon(true);
            
            rose.start();
            jack.start();
        }
    }
  • 相关阅读:
    Qt 的插件制作遇到的问题
    Qt实现原生Flow实现不了的Item错误排列效果,类似淘宝商品展示
    通过pro文件使Qt的build目录更清爽
    No rule to make target
    Qtcreator中printf()/fprintf()不显示问题处理方法
    QtCreator集成的MSVC套件有问题
    启动程序失败,路径或者权限错误
    图数据库 Nebula Graph 的数据模型和系统架构设计
    NLP(十六)轻松上手文本分类
    NLP(十五)让模型来告诉你文本中的时间
  • 原文地址:https://www.cnblogs.com/better-farther-world2099/p/9685673.html
Copyright © 2011-2022 走看看