zoukankan      html  css  js  c++  java
  • JAVA练手--线程(Thread)

    1. 查看线程是否还存活

    package tet;public class kk extends Thread{
        //1. 查看线程是否还存活
        public void run(){
            for(int i=0;i<5;i++)
                PrintName(2);
        }
        
        public void PrintName(int i){
            System.out.println(i+": "+Thread.currentThread().getName());
        }
        
        public static void main(String[] args)  {
            //把main建立出来当做子线程
            kk mythread = new kk();
            mythread.setName("Thread");
            
            System.out.println("before:"+mythread.isAlive());
            mythread.start();
            System.out.println("after:"+mythread.isAlive());
            
            for(int i=0;i<5;i++)
                mythread.PrintName(1);
            
            System.out.println("after:"+mythread.isAlive());
        }    
    }

    结果:

    before:false
    after:true
    1: main
    1: main
    1: main
    2: Thread
    2: Thread
    2: Thread
    2: Thread
    2: Thread
    1: main
    1: main
    after:false

    2. 状态监测

    notify是唤醒wait的,wait可以是自己运行,也可以是别的程序运行;

    结果:

    class MyThread extends Thread{
        
        boolean tmp_wait = false;  //是否等待
        
        MyThread(){
            System.out.println(getName()+" : construct");
        }
        public void run(){
            System.out.println(getName()+" : running");
            startWait();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
       //注意:必须要同步
       synchronized void startWait() {
              try {
                      while(!tmp_wait) {
                         System.out.println(getName()+" : waiting");
                         wait(); 
                      }
              }
              catch(InterruptedException exc) {
                 System.out.println(getName()+"interrupted");
              }
           }
       //注意:一定要同步,不同步不会编译报错,运行会出错,可以试试
       synchronized void notice() {
                 tmp_wait = true;
              notify();
           }
    
    }
    
    public class kk {
       public static void main(String args[]) {
           System.out.println("hello world");
           MyThread t = new MyThread();
           try{
               t.setName("thread1");
               showThreadStatus(1, t);
               t.start();
               Thread.sleep(50);  
               showThreadStatus(2, t);
               
               t.notice();  //唤醒wait
               Thread.sleep(50);  
               showThreadStatus(3, t);
               
               if(t.isAlive()){
                   showThreadStatus(4, t);
               }
               while(t.isAlive()){}
               
               showThreadStatus(5, t);
    
           }catch(InterruptedException e){
               e.printStackTrace();
           }
                  
       }
       
       static void showThreadStatus(int num, Thread t){
           System.out.println("num="+num+" ;name="+t.getName()+" ; status="+t.getState()+" ; live="+t.isAlive());
       }
    
    }

    结果:

    hello world
    Thread-0 : construct
    num=1 ;name=thread1 ; status=NEW ; live=false
    thread1 : running
    thread1 : waiting
    num=2 ;name=thread1 ; status=WAITING ; live=true
    num=3 ;name=thread1 ; status=TIMED_WAITING ; live=true
    num=4 ;name=thread1 ; status=TIMED_WAITING ; live=true
    num=5 ;name=thread1 ; status=TERMINATED ; live=false

    3. 中断线程

    package tet;
    
    
    public class kk implements Runnable {
           public void run() {
                  try {
                     System.out.println("in run() - 将运行 work2() 方法");
                     work2();
                     System.out.println("in run() - 从 work2() 方法回来");
                  }
                  catch (InterruptedException x) {
                     System.out.println("in run() - 中断 work2() 方法");
                     return;
                  }
                  System.out.println("in run() - 休眠后执行");
                  System.out.println("in run() - 正常离开");
               }
               public void work2() throws InterruptedException {
                  while (true) {
                     if (Thread.currentThread().isInterrupted()) {
                        System.out.println("C isInterrupted()=" + Thread.currentThread().isInterrupted());
                        Thread.sleep(2000);
                        System.out.println("D isInterrupted()=" + Thread.currentThread().isInterrupted());
                     }
                  }
               }
               public void work() throws InterruptedException {
                  while (true) {
                     for (int i = 0; i < 100000; i++) {
                        int j = i * 2;
                     }
                     System.out.println("A isInterrupted()=" + Thread.currentThread().isInterrupted());
                     if (Thread.interrupted()) {
                        System.out.println("B isInterrupted()=" + Thread.currentThread().isInterrupted());
                        throw new InterruptedException();
                     }
                  }
               }
               public static void main(String[] args) {
                   kk si = new kk();
                  Thread t = new Thread(si);
                  t.start();
                  try {
                     Thread.sleep(2000);
                  }
                  catch (InterruptedException x) {
                  }
                  System.out.println("in main() - 中断其他线程");
                  t.interrupt();
                  System.out.println("in main() - 离开");
               }
            }

    结果:

    in run() - 将运行 work2() 方法
    in main() - 中断其他线程
    in main() - 离开
    C isInterrupted()=true
    in run() - 中断 work2() 方法
  • 相关阅读:
    xampp 80端口被占用后这么办??解决了
    XAMPP配置基于虚拟目录、多域名的环境
    mysql 主从同步
    jquery插件
    Css绘制箭头实现代码
    Ubuntu下mount命令的好用处
    linux下IPTABLES配置详解
    java程序员网站
    1.Hibernate介绍
    1. Mybatis介绍
  • 原文地址:https://www.cnblogs.com/maogefff/p/8094426.html
Copyright © 2011-2022 走看看