Java传统线程中的的互斥技术

在JDK5以前,Java线程实现互斥有两种方式:同步代码块和同步方法。

方式一:同步代码块

package tradition;

public class TraditionalThreadSynchronized {

	public static void main(String[] args) {
		new TraditionalThreadSynchronized().init();
	}
	
	private void init() {
		//创建非静态内部类的对象必须依赖外部类的对象,
		//而该方法的调用表明已经有了一个对象
		final Outputer outputer = new Outputer();
		new Thread(new Runnable(){
			@Override
			public void run() {
				while(true) {
					try {
						Thread.sleep(10);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					outputer.ouput("zhangxiaoxiang");
				}
			}
			
		}).start();
		
		new Thread(new Runnable(){
			@Override
			public void run() {
				while(true) {
					try {
						Thread.sleep(10);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					outputer.ouput("lihuoming");
				}
			}
			
		}).start();
	}
	
	/**
	 * 自定义内部类
	 */
	class Outputer{
		/**
		 * 打印字符串中的每个字符
		 * @param name
		 */
		public void ouput(String name) {
			int len = name.length();
			//synchronized使用的必须是同一个对象,最好使用字节码确保唯一
			synchronized(Outputer.this) {
				for(int i =0; i < len; i ++) {
					System.out.print(name.charAt(i));
				}
				System.out.println();
			}
			
		}
	}
}


方式二:同步方法

package tradition;

public class TraditionalThreadSynchronized {

	public static void main(String[] args) {
		new TraditionalThreadSynchronized().init();
	}

	private void init() {
		// 创建非静态内部类的对象必须依赖外部类的对象,
		// 而该方法的调用表明已经有了一个对象
		final Outputer outputer = new Outputer();
		new Thread(new Runnable() {
			@Override
			public void run() {
				while (true) {
					try {
						Thread.sleep(10);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					outputer.ouput("zhangxiaoxiang");
				}
			}

		}).start();

		new Thread(new Runnable() {
			@Override
			public void run() {
				while (true) {
					try {
						Thread.sleep(10);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					outputer.ouput3("lihuoming");
				}
			}

		}).start();
	}

	/**
	 * 自定义内部类
	 */
	static class Outputer {
		/**
		 * 打印字符串中的每个字符,使用同步代码块
		 * 
		 * @param name
		 */
		public void ouput(String name) {
			int len = name.length();
			// synchronized使用的必须是同一个对象
			synchronized (Outputer.class) {
				for (int i = 0; i < len; i++) {
					System.out.print(name.charAt(i));
				}
				System.out.println();
			}
		}

		/**
		 * 打印字符串中的每个字符,
		 * 直接在方法上加synchronized,
		 * synchronized本质上用的也是this对象
		 * 
		 * @param name
		 */
		public synchronized void ouput2(String name) {
			int len = name.length();

			for (int i = 0; i < len; i++) {
				System.out.print(name.charAt(i));
			}
			System.out.println();
		}
		
		/**
		 * 静态同步方法块
		 * @param name
		 */
		public static synchronized void ouput3(String name) {
			int len = name.length();

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

		}
	}
}

上述代码中,output和output3可以实现同步,output2和output3无法实现同步,output和output2可以实现同步,注意有无static关键字的区别。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值