从头认识多线程-2.17 同步静态方法和静态代码块

这一章节我们来讨论一些同步静态方法和静态代码块。

代码清单

package com.ray.deepintothread.ch02.topic_17;

/**
 * 
 * @author RayLee
 *
 */
public class SynchClass {
	public static void main(String[] args) throws InterruptedException {
		ThreadOne threadOne = new ThreadOne();
		Thread thread = new Thread(threadOne);
		thread.start();
		ThreadTwo threadTwo = new ThreadTwo();
		Thread thread2 = new Thread(threadTwo);
		thread2.start();
	}
}

class ThreadOne implements Runnable {

	@Override
	public void run() {
		try {
			MyService.updateA();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}

class ThreadTwo implements Runnable {

	@Override
	public void run() {
		try {
			MyService.updateB();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}

class MyService {
	private static int id = 0;

	public synchronized static void updateA() throws InterruptedException {
		for (int i = 0; i < 5; i++) {
			System.out.println(Thread.currentThread().getName() + " " + id++);
			Thread.sleep(50);
		}
	}

	public synchronized static void updateB() throws InterruptedException {
		for (int i = 0; i < 5; i++) {
			System.out.println(Thread.currentThread().getName() + " " + id++);
			Thread.sleep(100);
		}
	}

}

输出:

Thread-0 0
Thread-0 1
Thread-0 2
Thread-0 3
Thread-0 4
Thread-1 5
Thread-1 6
Thread-1 7
Thread-1 8
Thread-1 9


同步静态方法,跟同步方法的性质和输出结果一致


package com.ray.deepintothread.ch02.topic_17;

/**
 * 
 * @author RayLee
 *
 */
public class SynchClass2 {
	public static void main(String[] args) throws InterruptedException {
		ThreadThree threadThree = new ThreadThree();
		Thread thread = new Thread(threadThree);
		thread.start();
		ThreadFour threadFour = new ThreadFour();
		Thread thread2 = new Thread(threadFour);
		thread2.start();
	}
}

class ThreadThree implements Runnable {

	@Override
	public void run() {
		try {
			MyService2.updateA();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}

class ThreadFour implements Runnable {

	@Override
	public void run() {
		try {
			MyService2.updateB();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}

class MyService2 {
	private static int id = 0;

	public static void updateA() throws InterruptedException {
		synchronized (MyService2.class) {
			for (int i = 0; i < 5; i++) {
				System.out.println(Thread.currentThread().getName() + " " + id++);
				Thread.sleep(50);
			}
		}
	}

	public static void updateB() throws InterruptedException {
		synchronized (MyService2.class) {
			for (int i = 0; i < 5; i++) {
				System.out.println(Thread.currentThread().getName() + " " + id++);
				Thread.sleep(100);
			}
		}
	}

}

输出:

Thread-0 0
Thread-0 1
Thread-0 2
Thread-0 3
Thread-0 4
Thread-1 5
Thread-1 6
Thread-1 7
Thread-1 8
Thread-1 9


在同步静态方法里面的代码块时,有一点需要注意的是,在普通方法当中,我们使用的this或者new object(),在静态代码块里面是需要使用.class文件作为监视器。


总结:这一章节展示了同步静态方法和静态代码块。


这一章节就到这里,谢谢

------------------------------------------------------------------------------------

我的github:https://github.com/raylee2015/DeepIntoThread


目录:http://blog.csdn.net/raylee2007/article/details/51204573



这一章节就到这里,谢谢

------------------------------------------------------------------------------------

我的github:https://github.com/raylee2015/DeepIntoThread


目录:http://blog.csdn.net/raylee2007/article/details/51204573

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值