从头认识多线程-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

你可能感兴趣的:(多线程,从头认识多线程,多线程)