首页 > 代码库 > synchronized:线程同步方法使用总结
synchronized:线程同步方法使用总结
synchronized:
1.多线程执行同一对象的synchronized函数,线程同步正确;
实例代码如下:
public class Test1 implements Runnable{
//定义同一对象
static Test2 action;
public static void main(String[] args) {
// TODO Auto-generated method stub
Class cls;
try {
cls = Class.forName("Test2");
action = (Test2) cls.newInstance();
Test1 t1 = new Test1();
for (int i=0;i<1;i++){
new Thread(t1).start();
new Thread(t1).start();
new Thread(t1).start();
new Thread(t1).start();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void run() {
// TODO Auto-generated method stub
try {
action.getRandomString();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class Test2 {
public static void main(String[] args) {
}
//调用同一对象的synchronized 函数,无需加statci修饰
public synchronized void getRandomString() throws InterruptedException {
System.out.println("$$$$$$$$$$$$$$$$$$");
Thread.sleep(1000);
Random random = new Random();
int aa = random.nextInt(10);
int bb = random.nextInt(10);
int cc = random.nextInt(10);
System.out.println(String.valueOf(aa)+String.valueOf(bb)+String.valueOf(cc));
System.out.println("############");
}
}
=====================================================
=====================================================
2.多线程执行不同对象的synchronized函数,线程同步错误;
实例代码如下:
public class Test1 implements Runnable{
public static void main(String[] args) {
try {
Test1 t1 = new Test1();
for (int i=0;i<100;i++){
new Thread(t1).start();
new Thread(t1).start();
new Thread(t1).start();
new Thread(t1).start();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void run() {
// TODO Auto-generated method stub
Class cls;
try {
cls = Class.forName("Test2");
Test2 action = (Test2) cls.newInstance();
action.getRandomString();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class Test2 {
public static void main(String[] args) {
}
/**
* @param args
* @throws InterruptedException
*/
//调用不同对象的synchronized 函数,需要加statci修饰,否则线程同步失败
public synchronized void getRandomString() throws InterruptedException {
System.out.println("$$$$$$$$$$$$$$$$$$");
Thread.sleep(1000);
Random random = new Random();
int aa = random.nextInt(10);
int bb = random.nextInt(10);
int cc = random.nextInt(10);
System.out.println(String.valueOf(aa)+String.valueOf(bb)+String.valueOf(cc));
System.out.println("############");
}
}
====================================================
=====================================================
3.多线程执行不同对象的synchronized static 函数,线程同步正确;
实例代码如下:
public class Test1 implements Runnable{
public static void main(String[] args) {
try {
Test1 t1 = new Test1();
for (int i=0;i<100;i++){
new Thread(t1).start();
new Thread(t1).start();
new Thread(t1).start();
new Thread(t1).start();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void run() {
// TODO Auto-generated method stub
Class cls;
try {
cls = Class.forName("Test2");
Test2 action = (Test2) cls.newInstance();
action.getRandomString();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class Test2 {
public static void main(String[] args) {
}
/**
* @param args
* @throws InterruptedException
*/
//调用不同对象的synchronized 函数,需要加statci修饰,否则线程同步失败
public synchronized static void getRandomString() throws InterruptedException {
System.out.println("$$$$$$$$$$$$$$$$$$");
Thread.sleep(1000);
Random random = new Random();
int aa = random.nextInt(10);
int bb = random.nextInt(10);
int cc = random.nextInt(10);
System.out.println(String.valueOf(aa)+String.valueOf(bb)+String.valueOf(cc));
System.out.println("############");
}
}
=========================================================
==========================================================
synchronized:线程同步方法使用总结