Java Synchronized- Thread Synchronization

Synchronization 用于控制多线程对共享资源的访问,它可以解决线程互相干扰以及一致性问题。可分为

  1. Process Synchronization
  2. Thread Synchronization

Thread Synchronization 又可以分为 mutual exclusive 和 inter-thread communication.

Mutual Exclusive 有三种实现方式

  1. by synchronized method
  2. by synchronized block
  3. by static synchronization

没有Synchronization 会造成的问题

package com.zhang.learn.effective_java;

public class synchronized_test {
	public static class Table{
		
		 public void printTable(int n) {
			for(int i=0; i<5; i++) {
				System.out.println(n*i);
				try {
				Thread.sleep(400);
				}catch(Exception e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public static class MyThread extends Thread{
		
		Table t;
		int table;
		
		public MyThread(Table t, int n) {
			this.t = t;
			table = n;
		}
		@Override
		public void run() {
			t.printTable(table);
		}
	}
	
	public static void main(String[] args) {
		Table ta = new Table();
		MyThread t1 = new MyThread(ta, 5);
		MyThread t2 = new MyThread(ta, 10);
		t1.start(); t2.start();
	}
}

输出如下

0
0
5
10
20
10
15
30
20
40

Synchronized method

If you declare any method as synchronized, it is known as synchronized method.
Synchronized method is used to lock an object for any shared resource.
When a thread invokes a synchronized method, it automatically acquires the lock for that object and releases it when the thread completes its task.
Synchronized 方法用与对一个对象加锁 当一个线程调用一个synchronized方法时, 它自动获取对象的锁 并且在执行完任务后 释放该锁

synchronized public void printTable(int n) {
			for(int i=0; i<5; i++) {
				System.out.println(n*i);
				try {
				Thread.sleep(400);
				}catch(Exception e) {
					e.printStackTrace();
				}
			}
		}

输出

0
5
10
15
20
0
10
20
30
40

如果对MyThread传入两个不同的table对象,则仍为交替打印

	public static void main(String[] args) {
		Table ta = new Table();
		Table ta2 = new Table();
		MyThread t1 = new MyThread(ta, 5);
		MyThread t2 = new MyThread(ta2, 10);
		t1.start(); t2.start();
	}
0
0
10
5
20
10
15
30
20
40

Synchronized block in java

Synchronized block can be used to perform synchronization on any specific resource of the method.
Suppose you have 50 lines of code in your method, but you want to synchronize only 5 lines, you can use synchronized block.
If you put all the codes of the method in the synchronized block, it will work same as the synchronized method.
Synchronized block 用于对方法中的任意指定部分执行同步操作
如方法中有50行代码,如果只想对其中的5行进行同步操作,就可以使用Synchronized block
如果把方法中的代码都放进synchronized block中,那就如同Synchronized method。

Synchronized block 用于对共享资源的对象加锁,并且作用范围比Synchronized method小

package com.zhang.learn.effective_java;

class Table{
	void printTable(int n) {
		synchronized(this) {
			for(int i =0 ;i<5; i++) {
				System.out.println(n*i);
				try {
					Thread.sleep(400);
				}catch(Exception e) {
					e.printStackTrace();
				}
			}
		}
	}
}

public class synchronized_block {
	
	public static void main(String[] args) {
		final Table ta = new Table();
		Thread t1 = new Thread() {
			public void run() {
				ta.printTable(5);
			}
		};
		Thread t2 = new Thread() {
			public void run() {
				ta.printTable(10);
			}
		};
		t1.start(); t2.start();
	}
	
}

Static synchronization

If you make any static method as synchronized, the lock will be on the class not on object.
如果把Synchronized加在静态方法上,那么锁的就是class而不是object

package com.zhang.learn.effective_java;

public class synchronized_test {
	public static class Table{
		
		synchronized public static void printTable(int n) {
			for(int i=0; i<5; i++) {
				System.out.println(n*i);
				try {
				Thread.sleep(400);
				}catch(Exception e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public static class MyThread extends Thread{
		
		Table t;
		int table;
		
		public MyThread(Table t, int n) {
			this.t = t;
			table = n;
		}
		@Override
		public void run() {
			t.printTable(table);
		}
	}
	
	public static void main(String[] args) {
		Table ta = new Table();
		Table ta2 = new Table();
		MyThread t1 = new MyThread(ta, 5);
		MyThread t2 = new MyThread(ta2, 10);
		MyThread t3 = new MyThread(ta2, 100);
		t1.start(); t2.start(); t3.start();
	}
}

输出

0
5
10
15
20
0
10
20
30
40
0
100
200
300
400

内容整理自

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值