zoukankan      html  css  js  c++  java
  • JAVA线程

    线程的各种状态如上图所看到的。

    对于wait/notify的測试,我将会留到 生产者消费者模式中实现。

    对于join、interrupt的測试例如以下:

    package com.huan;
    
    public class ThreadTest {
    	
    	public static void main(String[] args) throws Exception{
    //		joinTest();
    		interruptTest();
    	}
    	
    	public static void joinTest(){
    		new Thread(){
    			@Override
    			public void run() {
    				Thread t1 = new Thread(){
    					@Override
    					public void run() {
    						try {
    							Thread.sleep(3000);
    							System.out.println("//t1 thread");
    						} catch (InterruptedException e) {
    							System.out.println("//sleep interrupted");
    						}
    
    					}
    
    				};
    				t1.start();
    				try {
    					t1.join();
    				} catch (InterruptedException e) {
    					System.out.println("//join interrupted");
    				}
    				System.out.println("//out thread");
    			}
    		}.start();
    		
    		//t1 thread
    		//out thread
    	};
    	
    	public static void interruptTest(){
    		new Thread(){
    			@Override
    			public void run() {
    				Thread t1 = new Thread(){
    					@Override
    					public void run() {
    						try {
    							Thread.sleep(3000);
    							System.out.println("//t1 thread");
    						} catch (InterruptedException e) {
    							System.out.println("//sleep interrupted");
    						}
    
    					}
    
    				};
    				t1.start();
    				System.out.println("//out thread");
    				t1.interrupt();
    			}
    		}.start();
    		//out thread
    		//sleep exception
    	};
    	
    }
    

  • 相关阅读:
    六 . 爬虫 Xpath介绍和使用
    五. 爬虫 正则表达式
    四 . 爬虫 BeautifulSoup库参数和使用
    三 . 爬虫 url编码
    一 . 爬虫
    【HDU5952】Counting Cliques
    【HDU5521】Meeting
    【模板】回文自动机
    【CF1218E】Product Tuples
    【洛谷P2485】计算器
  • 原文地址:https://www.cnblogs.com/yutingliuyl/p/6791876.html
Copyright © 2011-2022 走看看