zoukankan      html  css  js  c++  java
  • Java总结之线程

    【线程的基本概念】
    线程是一个程序内部的顺序控制流。
    线程和进程的差别:
      每一个进程都有独立的代码和数据空间(进程上下文),进程间的切换会有较大的开销。
      线程能够看成是轻量级的进程,同一类线程共享代码和数据空间,每一个县城有独立的执行站和程序计数器(PC),线程切换的开销小。
      多进程:在操作系统中能同一时候执行多个任务(程序)
      多线程:在同一应用程序中有多个顺序流同一时候运行
    Java的线程是通过java.lang.Thread类来实现的。
    VM启动时会有一个由主方法(public static void main(){})所定义的线程。


    能够通过创建Thread的实例来创建新的线程。


    每一个线程都是通过摸个特定Thread对象所相应的方法run()来完毕其操作的,方法run()称为线程体。


    通过调用Thread类的start()方法来启动一个线程。
    【线程控制基本方法】
    isAlive() 推断线程是否还“活”着。即线程是否还未终止。


    getPriority() 获得线程的优先级数值
    setPriority() 设置现成的优先级数值
    Thread.sleep() 将当前线程睡眠指定毫秒数
    join() 调用某线程的该方法。将当前线程与该线程“合并”,即等待该线程结束,再恢复当前线程的执行。
    yield() 让出CPU,当前线程进入就绪队列等待调度。
    wait() 当前线程进入对象的wait pool。
    notify()/notifyAll() 唤醒对象的wait pool中的一个/全部等待线程。
    【sleep/join/yield方法】
    sleep方法
      能够调用Thread的静态方法:
      public static void sleep(long millis) throws InterruptedExcepion
      使得当前线程休眠(临时停止运行millis毫秒)
    因为是静态方法,sleep能够由类名直接调用:
      Thread.sleep(...)
    join方法
      合并某个线程
    yield方法
      让出CPU。给其它线程运行的机会
    join范例:

    public class TestJoin extends Thread{
    	public static void main(String[] args) {
    		MyThread t1 = new MyThread("t1");
    		t1.start();
    		try {t1.join();}
    		catch (InterruptedException e) {}
    		for(int i=1;i<=10;i++) {
    			System.out.println("i am main thread");
    		}
    	}
    }
    class MyThread extends Thread {
    	MyThread(String s) {super(s);}
    	public void run() {
    		for(int i=1;i<=10;i++) {
    			System.out.println("i am "+getName());
    			try {sleep(1000);}
    			catch (InterruptedException e) {return;}
    		}
    	}
    }


    yield范例:
    public class TestYield {
    	public static void main(String[] args) {
    		MyThread t1 = new MyThread("t1");
    		MyThread t2 = new MyThread("t2");
    		t1.start(); t2.start();
    	}
    }
    class MyThread extends Thread {
    	MyThread(String s) {super(s);}
    	public void run() {
    		for(int i=1;i<=100;i++) {
    			System.out.println(getName()+": "+i);
    			if(i%10==0) {
    				yield();
    			}
    		}
    	}
    }


    【线程的优先级别】
    Java提供一个线程调度器来监控程序中启动后进入就绪状态的全部线程。

    线程调度器依照线程的优先级决定应调度那个线程来运行。


    线程的优先级用数字表示,范围从1到10,一个线程的缺省优先级是5.
      Thread.MIN_PRIORITY = 1
      Thread.MAX_PRIORITY = 10
      Thread.NORM_PRIORITY = 5
    使用下述方法获得或设置线程对象的优先级。
      int getPriority();
      void setPriority(int new Priority);
    Priority范例:

    public class TestPriority {
    	public static void main(String[] args) {
    		Thread t1 = new Thread(new T1());
    		Thread t2 = new Thread(new T2());
    		t1.setPriority(Thread.NORM_PRIORITY + 3);
    		t1.start();
    		t2.start();
    	}
    }
    class T1 implements Runnable {
    	public void run() {
    		for(int i=0;i<100;i++) {
    			System.out.println("T1: "+i);
    		}
    	}
    }
    class T2 implements Runnable {
    	public void run() {
    		for(int i=0;i<100;i++) {
    			System.out.println("------T2: "+i);
    		}
    	}
    }


    【线程同步】
    在Java语言中,引入了对象相互排斥锁的概念,保证共享数据操作的完整性。

    每一个对象都相应于一个可称为"相互排斥锁"的标记,这个标记保证在随意时刻,仅仅能有一个线程訪问该对象。
    keywordsynchronized来与对象相互排斥锁联系。当某个对象synchronized修饰时,表明该对象在随意时刻仅仅能由一个线程訪问。


    synchronized 的用法:
    synchronized(this) {...}
    synchronized还能够放在方法声明中。表示整个方法为同步方法,比如:
      synchronized public void add(String name) {...}
    范例:

    public class TestSync implements Runnable {
    	Timer timer = new Timer();
    	public static void main(String[] args) {
    		TestSync test = new TestSync();
    		Thread t1 = new Thread(test);
    		Thread t2 = new Thread(test);
    		t1.setName("t1");
    		t2.setName("t2");
    		t1.start();
    		t2.start();
    	}
    	public void run() {
    		timer.add(Thread.currentThread().getName());
    	}
    }
    class Timer {
    	private static int num = 0;
    	public synchronized void add(String name) {
    		//synchronized (this) {
    			num ++;
    			try {Thread.sleep(1);}
    			catch (InterruptedException e) {}
    			System.out.println(name+", 你是第"+num+"个使用timer的线程");
    		//}
    	}
    }


    【死锁】
    范例:
    public class TestDeadLock implements Runnable {
    	public int flag = 1;
    	static Object o1 = new Object(), o2 = new Object();
    	public void run() {
    		System.out.println("flag=" + flag);
    		if(flag == 1) {
    			synchronized(o1) {
    				try {Thread.sleep(500);}
    				catch (Exception e) {
    					e.printStackTrace();
    				}
    				synchronized(o2) {
    					System.out.println("1");
    				}
    			}
    		}
    		if(flag == 0) {
    			synchronized(o2) {
    				try {Thread.sleep(500);}
    				catch (Exception e) {
    					e.printStackTrace();
    				}
    				synchronized(o1) {
    					System.out.println("0");
    				}
    			}
    		}
    	}
    	public static void main(String[] args) {
    		TestDeadLock td1 = new TestDeadLock();
    		TestDeadLock td2 = new TestDeadLock();
    		td1.flag = 1;
    		td2.flag = 0;
    		Thread t1 = new Thread(td1);
    		Thread t2 = new Thread(td2);
    		t1.start();
    		t2.start();
    	}
    }
    



  • 相关阅读:
    EFCore 中执行存储过程返回DataSet DataTable
    继 首次使用DoNetCore EFCore DbFirst 更新数据实体
    在DoNetCore MVC 中如何使用AutoMapper
    一款新的好用的SSH工具——FinalShell
    常用转义字符统计
    在MyEclipse安装Spket插件,用于jQuery代码提示
    Java连接Oracle数据库常用方法
    Java注解
    Eclipse怎样对项目进行重命名?
    Web Api试图加载格式不正确的程序,解决方法
  • 原文地址:https://www.cnblogs.com/llguanli/p/7089458.html
Copyright © 2011-2022 走看看