Java多线程那些事(二)

本文详细介绍了Java多线程中的核心方法,包括sleep()、wait()、notify()、notifyAll()及join()等,并通过示例代码展示了这些方法的具体使用方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本人有一篇文章介绍了线程的创建,以及线程的集中状态,只是大体介绍,现在深入研究下多线程的集中方法。

一:sleep()

先看源代码:

 /**	
     * Causes the currently executing thread to sleep (temporarily cease 
     * execution) for the specified number of milliseconds, subject to 
     * the precision and accuracy of system timers and schedulers. The thread 
     * does not lose ownership of any monitors.
     *
     * @param      millis   the length of time to sleep in milliseconds.
     * @exception  InterruptedException if any thread has interrupted
     *             the current thread.  The <i>interrupted status</i> of the
     *             current thread is cleared when this exception is thrown.
     * @see        Object#notify()
     */
    public static native void sleep(long millis) throws InterruptedException;

从源代码注释我们可以知道:
让当前的线程睡眠一定的时间,睡归睡,他还是一个线程,他没有丢掉它本身的特点。

来个实例:

package com.ThreadTest;

public class ThreadT extends Thread{
	public void run(){
		try {
			//当前线程睡眠1000毫秒,相当于睡眠1秒
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("ThreadT is running.....");
	}
	
	
	public static void main(String args[]){
		
		ThreadT tt= new ThreadT();
		Thread t= new Thread(tt);
		t.start();
		System.out.println("Main is running.....");
	}
}


运行结果当然是 Main is running.....

ThreadT is running....

因为ThreadT线程休眠了一秒钟,这样可以理解吧。

 

二:wait(),notify(),notifyAll(),Synchronized:

为什么将这四个放在一块呢,因为这四个共同组合可以创建很优秀的线程同步模型。

首先说一下类锁和对象锁,synchronized后,线程即获得对象锁或者类锁(static关键字修饰的时候),因为类似或对象锁只有一个,所以别的线程无法访问该方法,只能等到释放锁后,才可以获得锁执行代码。

看一个具体的例子:

package com.ThreadTest;

public class Thread1 implements Runnable {

	static int i = 0;

	public void run() {

	
		test();
		

	}

	public static synchronized void test() {
		System.out.println("Test is running"+i);
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("Test is end"+i);
		i++;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Thread t1 = new Thread(new Thread1());
		Thread t2 = new Thread(new Thread1());
		t1.start();
		t2.start();
	}

}


 

synchronized(this){}等价与public synchronized void method(){.....}
   同步分为类级别和对象级别,分别对应着类锁和对象锁。类锁是每个类只有一个,如果static的方法被synchronized关键字修饰,则在这个方法被执行前必须获得类锁;对象锁类同。

看了synchronized后,我们学习下wait和notify和notifyAll

首先,调用一个Object的wait与notify/notifyAll的时候,必须保证调用代码对该Object是同步的,也就是说必须在作用等同于synchronized(obj){......}的内部才能够去调用obj的wait与notify/notifyAll三个方法,否则就会报错
   java.lang.IllegalMonitorStateException: current thread not owner
   在调用wait的时候,线程自动释放其占有的对象锁,同时不会去申请对象锁。当线程被唤醒的时候,它才再次获得了去获得对象锁的权利。
   所以,notify与notifyAll没有太多的区别,只是notify仅唤醒一个线程并允许它去获得锁,notifyAll是唤醒所有等待这个对象的线程并允许它们去获得对象锁,只要是在synchronied块中的代码,没有对象锁是寸步难行的。其实唤醒一个线程就是重新允许这个线程去获得对象锁并向下运行。

    顺便说一下notifyall,虽然是对每个wait的对象都调用一次notify,但是这个还是有顺序的,每个对象都保存这一个等待对象链,调用的顺序就是这个链的顺序。其实启动等待对象链中各个线程的也是一个线程,在具体应用的时候,需要注意一下。

需要注意的概念是:

# 调用obj的wait(), notify()方法前,必须获得obj锁,也就是必须写在synchronized(obj) {...} 代码段内。

# 调用obj.wait()后,线程A就释放了obj的锁,否则线程B无法获得obj锁,也就无法在synchronized(obj) {...} 代码段内唤醒A。

# 当obj.wait()方法返回后,线程A需要再次获得obj锁,才能继续执行。

# 如果A1,A2,A3都在obj.wait(),则B调用obj.notify()只能唤醒A1,A2,A3中的一个(具体哪一个由JVM决定)。

# obj.notifyAll()则能全部唤醒A1,A2,A3,但是要继续执行obj.wait()的下一条语句,必须获得obj锁,因此,A1,A2,A3只有一个有机会获得锁继续执行,例如A1,其余的需要等待A1释放obj锁之后才能继续执行。

# 当B调用obj.notify/notifyAll的时候,B正持有obj锁,因此,A1,A2,A3虽被唤醒,但是仍无法获得obj锁。直到B退出synchronized块,释放obj锁后,A1,A2,A3中的一个才有机会获得锁继续执行。

三:Join():

(1)方法join也是实现同步的,看下源码:

    public final void join(long millis)throws InterruptedException
    Waits at most millis milliseconds for this thread to die. A timeout of 0 means to wait forever.
 大家能理解吗? 字面意思是等待一段时间直到这个线程死亡,我的疑问是那个线程,是它本身的线程还是调用它的线程的

 

package com.ThreadTest;

public class Thread3 extends Thread {

 public void run(){
  try {
   Thread.sleep(1000);
  } catch (InterruptedException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  System.out.println("Thread is running");
 }
 /**
  * @param args
  */
 public static void main(String[] args) throws InterruptedException {
  // TODO Auto-generated method stub
  Thread t= new Thread( new Thread3());
  
  t.start();
  t.join();
  System.out.println("Main is running");
  
 }

}


 

运行结果:为Thread is running

Main is running

代码二:

package com.ThreadTest;

public class Thread3 extends Thread {

	public void run(){
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("Thread is running");
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) throws InterruptedException {
		// TODO Auto-generated method stub
		Thread t= new Thread( new Thread3());
		
		t.start();
		t.join(500);//改变为500,
		System.out.println("Main is running");
		
	}

}


运行结果为:

main is running

Thread is running

通过上面的两个结果分析,我们可以知道,如果join()内部为0或者没有参数,main方法将一直等待Thread执行完毕后,才运行。

而当Join中的参数为1-无穷大的时候,main方法将等待一参数值那样的长度,就开始运行。

join的源代码如下:

/**
     * Waits at most <code>millis</code> milliseconds for this thread to 
     * die. A timeout of <code>0</code> means to wait forever. 
     *
     * @param      millis   the time to wait in milliseconds.
     * @exception  InterruptedException if any thread has interrupted
     *             the current thread.  The <i>interrupted status</i> of the
     *             current thread is cleared when this exception is thrown.
     */
    public final synchronized void join(long millis) 
    throws InterruptedException {
	long base = System.currentTimeMillis();
	long now = 0;

	if (millis < 0) {
            throw new IllegalArgumentException("timeout value is negative");
	}

	if (millis == 0) {
	    while (isAlive()) {
		wait(0);
	    }
	} else {
	    while (isAlive()) {
		long delay = millis - now;
		if (delay <= 0) {
		    break;
		}
		wait(delay);
		now = System.currentTimeMillis() - base;
	    }
	}
    }


可以看到,当millis>0的时候,底层是通过wait方法实现的,上面讲过wait方法,t.join(500)后,main就拿到了该对象的锁,然后继续执行下边的代码。很不错!

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值