多线程之同步Synchronized

在java中synchronized是多个线程共享同一段代码的锁。

多线程并发方法有:

1 加锁

2 信号量

3 阻塞---唤醒


当有多个线程并发执行同一块代码块时,加锁可以让一段时间内只有一个线程在执行,保证了业务的原子操作。

例如下面:

package andy.thread.traditional.test;

/**
 * @author Zhang,Tianyou
 * @version 2014年11月8日 下午11:02:53
 */

// 使用synchronized加同步锁

public class ThreadSynchronizedTest {

	public static void main(String[] args) {
		A a = new A();

		new Thread(new Runnable() {

			@Override
			public void run() {
				while (true) {
					try {
						Thread.sleep(10);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

					a.printfA(Thread.currentThread().getName()
							+ "hello, my is A");
				}

			}
		}).start();

		new Thread(new Runnable() {

			@Override
			public void run() {
				while (true) {
					try {
						Thread.sleep(10);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

					a.printfA(Thread.currentThread().getName()
							+ "hello, my is B");
				}

			}
		}).start();

	}

	static class A {
		public void printfA(String name) {

			for (int i = 0; i < name.length(); i++) {
				System.out.print(name.charAt(i));
			}

			System.out.println();

		}


	}

}



共同执行printfA方法时,如果不加锁,会导致输出紊乱。结果如下

Thread-1hello, my is B
Thread-1hello, my is B
Thread-0hello, my is A
ThThread-0hello, my is A
read-1hello, my is B
Thread-1helTlhreoad-0hello, my is A
, my is B
Thread-0hello, my is A
Thread-1hello, my is B
Thread-0hello, my is A

这是为了防止多线程并发执行时,确保只有一个在使用我们可以加同步锁。

synchronized(name){

// 加锁的代码块, name为锁标志

}


syschronized 可以加在方法里和方法内:

package andy.thread.traditional.test;

/**
 * @author Zhang,Tianyou
 * @version 2014年11月8日 下午11:02:53
 */

// 使用synchronized加同步锁

public class ThreadSynchronizedTest {

	public static void main(String[] args) {
		A a = new A();

		new Thread(new Runnable() {

			@Override
			public void run() {
				while (true) {
					try {
						Thread.sleep(10);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

					a.printfA(Thread.currentThread().getName()
							+ "hello, my is A");
				}

			}
		}).start();

		new Thread(new Runnable() {

			@Override
			public void run() {
				while (true) {
					try {
						Thread.sleep(10);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

					a.printfA(Thread.currentThread().getName()
							+ "hello, my is B");
				}

			}
		}).start();

	}

	static class A {
		public  void printfA(String name) {

			synchronized (A.class) {
				for (int i = 0; i < name.length(); i++) {
					System.out.print(name.charAt(i));
				}

				System.out.println();
			}
			

		}

		public synchronized void printfB(String name) {

			for (int i = 0; i < name.length(); i++) {
				System.out.print(name.charAt(i));
			}

			System.out.println();

		}
	}

}


运行效果如下:

Thread-1hello, my is B
Thread-0hello, my is A
Thread-1hello, my is B
Thread-0hello, my is A
Thread-1hello, my is B
Thread-0hello, my is A
Thread-1hello, my is B
Thread-0hello, my is A
Thread-1hello, my is B
Thread-0hello, my is A
Thread-1hello, my is B
Thread-0hello, my is A
Thread-1hello, my is B
Thread-0hello, my is A
Thread-1hello, my is B



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值