synchronized Java语言的关键字,当它用来修饰一个方法或者一个代码块的时候,能够保证在同一时刻最多只有一个线程执行该段代码。
先看几个例子:
实例一:
import java.util.ArrayList;
import java.util.List;
public class MyThread {
public void printNum(List list) {
synchronized (list) {
if (null != list) {
for (int i = 0; i < list.size(); i++) {
System.out.println(Thread.currentThread().getName() + ":"+ list.get(i));
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
public static List getList() {
List list = new ArrayList();
for (int i = 1; i <= 5; i++) {
list.add(i);
}
return list;
}
public static void main(String[] args) {
final MyThread mt = new MyThread();
final MyThread mt1 = new MyThread();
final List list = mt.getList();
Thread t1 = new Thread(new Runnable(){
public void run() {
mt.printNum(list);
}
}, "t1");
Thread t2 = new Thread(new Runnable(){
public void run() {
mt1.printNum(list);
}
}, "t2");
t1.start();
t2.start();
}
}
输出结果:
t1:1
t1:2
t1:3
t1:4
t1:5
t2:1
t2:2
t2:3
t2:4
t2:5
实例二:
import java.util.ArrayList;
import java.util.List;
public class MyThread {
public void printNum(List list) {
synchronized (list) {
if (null != list) {
for (int i = 0; i < list.size(); i++) {
System.out.println(Thread.currentThread().getName() + ":"+ list.get(i));
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
public static List getList() {
List list = new ArrayList();
for (int i = 1; i <= 5; i++) {
list.add(i);
}
return list;
}
public static void main(String[] args) {
final MyThread mt = new MyThread();
final MyThread mt1 = new MyThread();
final List list = mt.getList();
final List list1 = new ArrayList();
for (int i = 1; i <= 5; i++) {
list1.add(i);
}
Thread t1 = new Thread(new Runnable(){
public void run() {
mt.printNum(list);
}
}, "t1");
Thread t2 = new Thread(new Runnable(){
public void run() {
mt1.printNum(list1);
}
}, "t2");
t1.start();
t2.start();
}
}
输出结果:
t1:1
t2:1
t1:2
t2:2
t2:3
t1:3
t1:4
t2:4
t2:5
t1:5
实例三:
import java.util.ArrayList;
import java.util.List;
public class MyThread {
public void printNum(List list) {
synchronized (this) {
if (null != list) {
for (int i = 0; i < list.size(); i++) {
System.out.println(Thread.currentThread().getName() + ":"+ list.get(i));
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
public static List getList() {
List list = new ArrayList();
for (int i = 1; i <= 5; i++) {
list.add(i);
}
return list;
}
public static void main(String[] args) {
final MyThread mt = new MyThread();
final MyThread mt1 = new MyThread();
final List list = mt.getList();
final List list1 = new ArrayList();
for (int i = 1; i <= 5; i++) {
list1.add(i);
}
Thread t1 = new Thread(new Runnable(){
public void run() {
mt.printNum(list);
}
}, "t1");
Thread t2 = new Thread(new Runnable(){
public void run() {
mt1.printNum(list1);
}
}, "t2");
t1.start();
t2.start();
}
}
输出结果:
t1:1
t2:1
t1:2
t2:2
t1:3
t2:3
t1:4
t2:4
t1:5
t2:5
实例四:
import java.util.ArrayList;
import java.util.List;
public class MyThread {
public void printNum(List list) {
synchronized (this) {
if (null != list) {
for (int i = 0; i < list.size(); i++) {
System.out.println(Thread.currentThread().getName() + ":"+ list.get(i));
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
public static List getList() {
List list = new ArrayList();
for (int i = 1; i <= 5; i++) {
list.add(i);
}
return list;
}
public static void main(String[] args) {
final MyThread mt = new MyThread();
final MyThread mt1 = new MyThread();
final List list = mt.getList();
final List list1 = new ArrayList();
for (int i = 1; i <= 5; i++) {
list1.add(i);
}
Thread t1 = new Thread(new Runnable(){
public void run() {
mt.printNum(list);
}
}, "t1");
Thread t2 = new Thread(new Runnable(){
public void run() {
mt.printNum(list1);
}
}, "t2");
t1.start();
t2.start();
}
}
输出结果:
t1:1
t1:2
t1:3
t1:4
t1:5
t2:1
t2:2
t2:3
t2:4
t2:5
实例五:
public class MyThread {
public void printNum(List list) {
synchronized (list) {
if (null != list) {
for (int i = 0; i < list.size(); i++) {
System.out.println(Thread.currentThread().getName() + ":"+ list.get(i));
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
public static List getList() {
List list = new ArrayList();
for (int i = 1; i <= 5; i++) {
list.add(i);
}
return list;
}
public static void main(String[] args) {
final MyThread mt = new MyThread();
final MyThread mt1 = new MyThread();
final List list = mt.getList();
final List list1 = new ArrayList();
for (int i = 1; i <= 5; i++) {
list1.add(i);
}
Thread t1 = new Thread(new Runnable(){
public void run() {
mt.printNum(list);
}
}, "t1");
Thread t2 = new Thread(new Runnable(){
public void run() {
mt.printNum(list1);
}
}, "t2");
t1.start();
t2.start();
}
}
输出结果:
t1:1
t2:1
t1:2
t2:2
t2:3
t1:3
t1:4
t2:4
t2:5
t1:5
由以上实例可以得出,不同的线程调用的必须是同一个对象,并且该对象与synchronized 锁的对象是同一个类时,才能够保证在同一时刻最多只有一个线程执行该段代码。