• 线程的2中基本用法


    这里列出了java线程的2种基本使用方法:


    Runner接口

    public class TestThread1 {
    	public static void main(String args[]) {
    		Runner1 r = new Runner1();
    		Thread t = new Thread(r);
    		t.start();
    		
    		for(int i=0; i<15; i++) {
    			System.out.println("Main Thread:------" + i);
    		}
    	}
    }
    
    class Runner1 implements Runnable {
    
    	public void run() {
    		for(int i=0; i<15; i++) {	
    			System.out.println("Runner1 :" + i);
    		}
    	}
    }
    /**
    结果:
    Main Thread:------0
    Runner1 :0
    Main Thread:------1
    Runner1 :1
    Main Thread:------2
    Runner1 :2
    Main Thread:------3
    Runner1 :3
    Main Thread:------4
    Runner1 :4
    Main Thread:------5
    Runner1 :5
    Main Thread:------6
    Runner1 :6
    Main Thread:------7
    Runner1 :7
    Main Thread:------8
    Runner1 :8
    Main Thread:------9
    Runner1 :9
    Main Thread:------10
    Main Thread:------11
    Runner1 :10
    Main Thread:------12
    Runner1 :11
    Main Thread:------13
    Runner1 :12
    Main Thread:------14
    Runner1 :13
    Runner1 :14
    **/


    Thread

    public class TestThread1 {
    	public static void main(String args[]) {
    		Runner1 r = new Runner1();
    		r.start();
    
    		
    		for(int i=0; i<15; i++) {
    			System.out.println("Main Thread:------" + i);
    		}
    	}
    }
    
    
    class Runner1 extends Thread {
    	public void run() {
    		for(int i=0; i<15; i++) {	
    			System.out.println("Runner1 :" + i);
    		}
    	}
    }
    /**
    结果:
    Main Thread:------0
    Main Thread:------1
    Runner1 :0
    Main Thread:------2
    Runner1 :1
    Main Thread:------3
    Runner1 :2
    Main Thread:------4
    Runner1 :3
    Main Thread:------5
    Runner1 :4
    Main Thread:------6
    Runner1 :5
    Main Thread:------7
    Runner1 :6
    Main Thread:------8
    Runner1 :7
    Main Thread:------9
    Runner1 :8
    Main Thread:------10
    Runner1 :9
    Runner1 :10
    Runner1 :11
    Runner1 :12
    Runner1 :13
    Runner1 :14
    Main Thread:------11
    Main Thread:------12
    Main Thread:------13
    Main Thread:------14
    **/

    其实一个cup在同一时间上只有一个线程执行,只是执行速度很快,给人一种同时执行的感觉。



    线程有一些方法及知识点:

    sleep()

    interrupt()

    stop()

    join()

    yield()

    优先级设置




  • 相关阅读:
    Atom 和 markdown 基本使用
    c++11 正则表达式基本使用
    Emacs 之窗口管理
    Emacs 之列编辑模式
    Emacs 之查看帮助
    使用 json_in_java
    Linux服务 httpd
    Linux 编译安装BIND
    Kerberos
    Linux服务 DNS&BIND
  • 原文地址:https://www.cnblogs.com/riskyer/p/3341718.html
Copyright © 2020-2023  润新知