您的位置:首页 > 编程语言 > Java开发

java多线程-synchronized对象和方法的区别

2017-10-14 10:51 295 查看
public class App {
public static void main(String[] args) {
System.out.println("Synchronized Objects: ");
Worker worker = new Worker();
worker.main();
System.out.println("Synchronized Methods: ");
WorkerMethodsSynchronized worker2 = new WorkerMethodsSynchronized();
worker2.main();
}
}


public class Worker {
private Random random=new Random();
private final Object lock1=new Object();
private final Object lock2=new Object();

private List<Integer> list1=new ArrayList<>();
private List<Integer> list2=new ArrayList<>();

public void stageOne(){
synchronized (lock1){
try {
Thread.sleep(1);
}catch (InterruptedException e){
e.printStackTrace();
}
list1.add(random.nextInt(100));
}
}
public void stageTwo(){
synchronized (lock2){
try {
Thread.sleep(1);
}catch (InterruptedException e){
e.printStackTrace();
}
list2.add(random.nextInt(100));
}
}
public void process(){
for(int i=0;i<1000;i++){
stageOne();
stageTwo();
}
}

public  void main() {
System.out.println("Starting ...");
long start=System.currentTimeMillis();
Thread t1=new Thread(new Runnable() {
@Override
public void run() {
process();
}
});
Thread t2=new Thread(new Runnable() {
@Override
public void run() {
process();
}
});
t1.start();
t2.start();
try {
t1.join();
t2.join();
}catch (InterruptedException e){
e.printStackTrace();
}
long end=System.currentTimeMillis();
System.out.println("time taken "+(end-start));
System.out.println("List1:"+list1.size()+" List2:"+list2.size());
}
}


public class WorkerMethodsSynchronized {

private Random random = new Random();

private List<Integer> list1 = new ArrayList<>();
private List<Integer> list2 = new ArrayList<>();

/**
* synchronized, methods use different data (list1 list2) so by synchronized
* methods if one thread runs the stageOne other thread cannot run stageTwo
* at the same time because that same locks are used. Solution is using two
* lock Object for two shared data.
*/
public synchronized void stageOne() {
try {
//do your work here
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
list1.add(random.nextInt(100));
}

public synchronized void stageTwo() {
try {
//do your work here
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
list2.add(random.nextInt(100));
}

public void process() {
for (int i = 0; i < 1000; i++) {
stageOne();
stageTwo();
}
}

public void main() {
System.out.println("Starting ...");
long start = System.currentTimeMillis();
Thread t1 = new Thread(new Runnable() {
public void run() {
process();
}
});

Thread t2 = new Thread(new Runnable() {
public void run() {
process();
}
});

t1.start();
t2.start();

try {
t1.join();
t2.join();
} catch (InterruptedException ignored) {}

long end = System.currentTimeMillis();

System.out.println("Time taken: " + (end - start));
System.out.println("List1: " + list1.size() + "; List2: " + list2.size());
}
}


输出:

Synchronized Objects:

Starting …

time taken 2598

List1:2000 List2:2000

Synchronized Methods:

Starting …

Time taken: 5184

List1: 2000; List2: 2000

可见Synchronized 方法更耗时间

解析:

就是弄个循环往两个list加入随机值
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐