zoukankan      html  css  js  c++  java
  • 菜鸡的Java笔记

    线程常用操作方法
            线程的命名操作,线程的休眠,线程的优先级
        
            线程的所有操作方法几乎都在 Thread 类中定义好了
            
        线程的命名和取得
            从本质上来讲多线程的运行状态并不是固定的。所以来讲爱那个要想确定线程的执行,唯一的区别就在于线程的名称上
            在起名的时候就应该尽可能的避免重名,或者避免修改名称
            在 Thread 类中提供有如下的方法可以实现线程名称的操作:
                构造方法: public Thread(Runnable target, String name)
                设置名字: public final void setName(String name)
                取得名字: public final String getName()
                
            既然线程的执行本身是不确定的状态,所以如果要取得线程名字的话,那么唯一能做的就是取得当前的线程名字
            所以在 Thread 类里面提供有这样的方法: public static Thread currentThread()
            
            范例:线程的命名和取得

    package cn.mysterious.study3;
    
    class MyThread implements  Runnable{
    
        @Override
        public void run() {
            // TODO Auto-generated method stub
            for (int i = 0; i < 10; i++) {
                System.out.println(Thread.currentThread().getName() + ",i = " + i);
            }
        }
        
    }
    
    public class StudyThread {
    
        public static void main(String[] args) throws Exception {
            MyThread mt = new MyThread();
            new Thread(mt,"线程A").start();
            new Thread(mt).start();
            new Thread(mt).start();
        }
    
    }

               
                如果在设置线程对象是没有设置具体的名字,那么就采用一个默认的名字进行定义
                
            范例:观察代码

    package cn.mysterious.study3;
    
    class MyThread implements  Runnable{
    
        @Override
        public void run() {
                System.out.println("MyThread 线程类:" + Thread.currentThread().getName());
        }
        
    }
    
    public class StudyThread {
    
        public static void main(String[] args) throws Exception {
            MyThread mt = new MyThread();
            new Thread(mt).start(); // 线程启动调用 run() 方法
            mt.run(); // 直接通过对象调用 run() 方法
        }
    
    }
    /*
        结果:
        MyThread 线程类:main
        MyThread 线程类:Thread-0
    */

                   
                MyThread 线程类:main ( mt.run(); )
                MyThread 线程类:Thread-0 (new Thread(mt).start();)
                
            线程一定是依附在进程存在的,但是现在的进程在哪里呢?
                每当使用java命令在JVM上解释某一个程序执行的时候,那么都会默认的启动一个JVM的进程,而主方法只是这进程中的一个线程,所以整个程序一直都跑在线程的运行机制上
                
                每个JVM至少会启动两个线程:主线程,GC线程
                
        线程的休眠
            如果要想让某些线程延缓执行,俺么就可以使用休眠的方式来进行处理
            在 Thread 类里面提供有如下的休眠操作:
                休眠方法: public static void sleep(long millis,int nanos)throws InterruptedException
                如果休眠的时间没到就停止休眠了,那么就会产生中断异常
            范例:观察休眠

    package cn.mysterious.study3;
    
    class MyThread implements  Runnable{
    
        @Override
        public void run() {
            // TODO Auto-generated method stub
            for (int i = 0; i < 10; i++) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + ",i = " + i);
            }
        }
        
    }
    public class StudyThread {
    
        public static void main(String[] args) throws Exception {
            MyThread mt = new MyThread();
            new Thread(mt,"线程A").start();
            new Thread(mt,"线程B").start();
            new Thread(mt,"线程C").start();
        }
    
    }

               
                以上的代码执行中感觉像是所有的线程对象都同时休眠了。但是严格来讲不是同时,是有先后顺序的,只不过这个顺序小一点而已
            

    package cn.mysterious.study3;
    
    class MyThread implements  Runnable{
    
        @Override
        public void run() {
            // TODO Auto-generated method stub
            for (int i = 0; i < 100; i++) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + ",i = " + i);
            }
        }
        
    }
    public class StudyThread {
    
        public static void main(String[] args) throws Exception {
            MyThread mt = new MyThread();
            Thread t = new Thread(mt,"线程A");
            t.start();
            Thread.sleep(2000);
            t.interrupt(); // 中断
            
        }
    }

               
                后续会使用休眠来进行线程的分析

        线程的优先级
            从理论上来讲优先级越高的线程越有可能先执行。而在 Thread 类里面定义有一下的优先级操作方法:
                设置优先级: public final void setPriority(int newPriority)
                取得优先级: public final int getPriority()
            而对于优先级一共定义有三种:
                最高优先级: public static final int MAX_PRIORITY:    10
                中等优先级: public static final int NORM_PRIORITY:    5
                最小优先级: public static final int MIN_PRIORITY:    1

            范例:观察优先级

    package cn.mysterious.study3;
    
    class MyThread implements  Runnable{
    
        @Override
        public void run() {
            // TODO Auto-generated method stub
            for (int i = 0; i < 100; i++) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + ",i = " + i);
            }
        }
        
    }
    public class StudyThread {
    
        public static void main(String[] args) throws Exception {
            MyThread mt = new MyThread();
            Thread t1 = new Thread(mt,"线程A");
            Thread t2 = new Thread(mt,"线程B");
            Thread t3 = new Thread(mt,"线程C");
            // 设置优先级
            t1.setPriority(Thread.MAX_PRIORITY);
            t2.setPriority(Thread.MIN_PRIORITY);
            t3.setPriority(Thread.MIN_PRIORITY);
            t1.start();
            t2.start();
            t3.start();
        }
    
    }

               
            范例:主线程的优先级是什么呢?

        public class StudyThread {
    
            public static void main(String[] args) throws Exception {
                System.out.println(Thread.currentThread().getPriority());
            }
    
        }

               
                可以发现主线程属于中等优先级或者叫一般优先级
                
        总计
            1.线程要有名字, Thread.currentThread 取得当前线程
            2.线程的休眠是有先后顺序的
            3.理论上线程的优先级越高越有可能先执行

  • 相关阅读:
    DML-DDL-DCL
    FastDFS常见场景模拟
    如何定义软件版本
    鸟哥的linux私房菜学习-(七)改变文件属性与权限
    鸟哥的linux私房菜学习-(六)Linux 文件权限介绍
    二、基本语法
    一、JavaSE语言概述
    鸟哥的linux私房菜学习-(五)补充:重点回顾
    鸟哥的linux私房菜学习-(五)Linux系统的在线求助man page与info page
    鸟哥的linux私房菜学习-(四)linux命令的基本概念
  • 原文地址:https://www.cnblogs.com/mysterious-killer/p/10124090.html
Copyright © 2011-2022 走看看