关于synchronized
2012-09-12 14:01
141 查看
今天在CSDN看到有人问关于synchronized的帖子,随手写了一个回帖了,然后发现了问题,记录一下。
原帖:http://topic.csdn.net/u/20120912/10/3898dec6-cc46-4587-92ed-0be1a40ebb08.html
在我第一次回答的里面:代码如下(稍微修改了一下):我想说的都在代码的注释里。
public class SynDemo
{
public static void main(String[] args)
{
Test test = new Test();
new T1(test).start();
new T1(test).start();
}
}
class T1 extends Thread
{
private Test test;
T1(Test test)
{
this.test = test;
}
@Override
public void run()
{
//在这里循环调用test,每次调用完就释放掉了Test的锁,所以下一个test方法是能进去的,所以就达不到预期效果。
for(int i = 0; i < 10; i ++)
{
try
{
Thread.sleep((long)(Math.random() * 500));
}
catch (InterruptedException e)
{
e.printStackTrace();
}
test.test();
}
}
}
class Test
{
synchronized public void test()
{
System.out.println(Thread.currentThread().getName() + " hello");
}
}第二次回答的代码:
public class Syn
{
public static void main(String[] args)
{
D d = new D();
new MyThread(d).start();
new MyThread(d).start();
}
}
class MyThread extends Thread
{
private D d;
public MyThread(D d)
{
this.d = d;
}
@Override
public void run()
{
d.test();
}
}
class D
{
synchronized public void test()
{
//在这里循环,每次循环完并不释放掉锁,所以能达到预期的效果。
for(int i = 0 ; i < 10; i ++)
{
try
{
Thread.sleep((long)(Math.random() * 500));
}
catch (InterruptedException e)
{
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " hello");
}
}
}
总结一下,由于不细心,导致错误,不过自己对synchronized的理解又更深了一步。
原帖:http://topic.csdn.net/u/20120912/10/3898dec6-cc46-4587-92ed-0be1a40ebb08.html
在我第一次回答的里面:代码如下(稍微修改了一下):我想说的都在代码的注释里。
public class SynDemo
{
public static void main(String[] args)
{
Test test = new Test();
new T1(test).start();
new T1(test).start();
}
}
class T1 extends Thread
{
private Test test;
T1(Test test)
{
this.test = test;
}
@Override
public void run()
{
//在这里循环调用test,每次调用完就释放掉了Test的锁,所以下一个test方法是能进去的,所以就达不到预期效果。
for(int i = 0; i < 10; i ++)
{
try
{
Thread.sleep((long)(Math.random() * 500));
}
catch (InterruptedException e)
{
e.printStackTrace();
}
test.test();
}
}
}
class Test
{
synchronized public void test()
{
System.out.println(Thread.currentThread().getName() + " hello");
}
}第二次回答的代码:
public class Syn
{
public static void main(String[] args)
{
D d = new D();
new MyThread(d).start();
new MyThread(d).start();
}
}
class MyThread extends Thread
{
private D d;
public MyThread(D d)
{
this.d = d;
}
@Override
public void run()
{
d.test();
}
}
class D
{
synchronized public void test()
{
//在这里循环,每次循环完并不释放掉锁,所以能达到预期的效果。
for(int i = 0 ; i < 10; i ++)
{
try
{
Thread.sleep((long)(Math.random() * 500));
}
catch (InterruptedException e)
{
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " hello");
}
}
}
总结一下,由于不细心,导致错误,不过自己对synchronized的理解又更深了一步。
相关文章推荐
- iOS开发笔记--关于 @synchronized,这儿比你想知道的还要多
- iOS 中关于@synchronized的一点思考
- 再谈关于 java 线程,synchronized,wait,notify
- 关于synchronized关键字的问题
- 关于synchronized(this)
- 关于synchronized、wait、notify已经notifyAll的使用
- 关于synchronized、wait、notify已经notifyAll的使用
- 关于@synchronized(self)的用法
- 关于 @synchronized,这儿比你想知道的还要多
- 关于synchronized具备原子性的问题
- java关于synchronized的某些知识
- 关于synchronized(this)中this指的是什么意思
- 关于synchronized一个经典的例子
- 同步——synchronized机制、Lock和Conditon机制和关于线程中断
- 关于synchronized的几点说明
- [绍棠] 关于 @synchronized
- 关于synchronized、wait、notify已经notifyAll的使用
- 关于synchronized(obj)的讨论
- 关于多线程同步的初步教程--使用synchronized [推荐]
- Java【多线程知识总结(7)】多线程同步问题-关于synchronized代码块和synchronized方法的应用