zoukankan      html  css  js  c++  java
  • Java高级部分--线程(4)

    一.线程

    线程:线程是一个程序内部的顺序控制流

    多线程:一个进程中的多个同时执行的任务是多线程(交替执行)

    进程:电脑中的多个执行的任务

                                                               线程的工作流程生命周期

    线程原理:

        1.创建线程

        2.调用start方法激活线程

        3.就绪状态(没运行)

        4.cpu调度到该线程后进入运行状态

        5.也可以调用sleep方法,进入暂时阻塞状态  ,时间过后,再次进入就绪状态sleep(参数是毫秒)

        6.代码执行结束自动停止,自动死掉,不用close

    创建线程:

                 1.自定义类,继承Thread

                 2.自定义类,实现Runnable接口

    创建一个类继承Thread类,重写run方法,thread类中是已有start方法的

    ThreadA a = new ThreadA(); 
    a.start();

    Runnable接口自动带有run方法,但是没有start方法,所以要实例化thread对象,再调用start方法,调用start方法就会执行run里头的动作了

    ThreadB b = new ThreadB();
    Thread thread = new Thread(b);
    thread.start();

    优先级低,最后执行 setPriority(1);

    •需求:ThreadA类中、ThreadB类中、程序入口主方法中,ThreadA休眠时间一秒,ThreadB休眠时间为半秒,交替分别循环1000次

     1 ThreadA类
     2 
     3 package thread1;
     4 
     5 public class ThreadA extends Thread {   //线程类
     6     
     7     
     8     @Override
     9     public void run() {
    10         
    11          for(int i=0;i<1000;i++){
    12              
    13              if(i%2==1){            //A线程执行到奇数时,休眠1秒
    14                  
    15                 try {
    16                     Thread.sleep(1000);   //线程休眠
    17                 } catch (InterruptedException e) {   
    18                     // TODO Auto-generated catch block
    19                     e.printStackTrace();
    20                 }
    21              }
    22              
    23              System.out.println("---------线程A的执行动作"+i);   
    24          }
    25         
    26     }
    27 
    28 }
     1 ThreadB类
     2 
     3 package thread1;
     4 
     5 public class ThreadB implements Runnable {
     6 
     7     @Override
     8     public void run() {    //在调用start方法时执行的动作代码
     9         
    10          for(int i=0;i<1000;i++){
    11              
    12              if(i%3==0){
    13                  
    14                  try {
    15                     Thread.sleep(500);
    16                 } catch (InterruptedException e) {   
    17                     // TODO Auto-generated catch block
    18                     e.printStackTrace();
    19                 }
    20              }
    21              System.out.println("线程B的执行动作"+i);   
    22          }
    23 
    24     }
    25 
    26 }
     1 主方法
     2 
     3 package thread1;
     4 
     5 public class Main {
     6 
     7     /**
     8      * @param args
     9      */
    10     public static void main(String[] args) {
    11     
    12 
    13         for (int i = 0; i < 1000; i++) {
    14             System.out.println("***主方法执行的动作***" + i);
    15         }
    16 
    17         ThreadA a = new ThreadA(); // 线程是死的
    18         a.start();
    19 
    20         ThreadB b = new ThreadB();
    21         Thread thread = new Thread(b);
    22         thread.start();
    23     }
    24 
    25 }

    执行结果

    •需求:计时功能

     1 ShowDate类
     2 
     3 package thread1;
     4 
     5 import java.text.SimpleDateFormat;
     6 import java.util.Date;
     7 
     8 public class ShowDate extends Thread {    //计时
     9     
    10     @Override
    11     public void run() {
    12          //         int  x=0;
    13         
    14            while(true){
    15                
    16                Date   date=new Date();
    17                SimpleDateFormat  sdf=new SimpleDateFormat("yyyy/MM/dd   hh:mm:ss");
    18                System.out.println(sdf.format(date));
    19     //           x++;
    20                
    21        //           if(x>10){
    22        //                break;
    23       //               }
    24                try {
    25                 Thread.sleep(1000);
    26                 
    27             } catch (InterruptedException e) {
    28                 // TODO Auto-generated catch block
    29                 e.printStackTrace();
    30             }
    31    
    32            }
    33 
    34     }
    35 
    36 }
     1 主方法
     2 
     3 package thread1;
     4 
     5 public class Main {
     6 
     7     /**
     8      * @param args
     9      */
    10     public static void main(String[] args) {
    11         
    12           ShowDate   sDate=new ShowDate();
    13            sDate.start();
    14 
    15     }
    16 
    17 }

    执行结果

  • 相关阅读:
    mysql 历史版本下载
    mysql 5.7 版本 You must reset your password using ALTER USER statement before executing this statement报错处理
    5.7 zip 版本的安装 以及遇到的坑
    mysql 5.6zip版本的卸载与5.7 zip 版本的安装
    mysql数据库的备份与还原
    本地Navicat连接docker里的mysql
    docker修改数据库密码
    docker 在push镜像到本地registry出现的500 Internal Server Error
    linux 没有界面内容显示不全解决办法
    json与map互相转换
  • 原文地址:https://www.cnblogs.com/Pioneer-HengYu/p/6639603.html
Copyright © 2011-2022 走看看