黑马程序员技术交流社区

标题: 静态同步函数的锁不是对象吗???? [打印本页]

作者: 刘俊佳    时间: 2012-8-10 10:15
标题: 静态同步函数的锁不是对象吗????
以下是代码;
  1. package thread;

  2. public class Ticket3 implements Runnable
  3. {
  4. private static int tick=100;
  5. Object obj=new Object();
  6. boolean flag=true;

  7. @Override
  8. public void run()
  9. {
  10. if(flag)
  11. {
  12. while(true)
  13. {
  14. synchronized(Ticket.class)
  15. {
  16. if(tick>0)
  17. {
  18. try {
  19. Thread.sleep(10);
  20. } catch (InterruptedException e) {
  21. }
  22. }
  23. System.out.println(Thread.currentThread().getName() + "....code:"+ tick--);
  24. }

  25. }
  26. }

  27. else
  28. while(true)
  29. show();
  30. }
  31. public static synchronized void show()
  32. {
  33. if (tick > 0) {
  34. try {
  35. Thread.sleep(10);
  36. } catch (InterruptedException e) {

  37. }
  38. System.out.println(Thread.currentThread().getName() + "....show:"+ tick--);
  39. }
  40. }
  41. }

复制代码
  1. package thread;

  2. public class ThisLockDemo2
  3. {
  4. public static void main(String[] args)
  5. {

  6. Ticket3 t=new Ticket3();

  7. Thread t1=new Thread(t);
  8. Thread t2=new Thread(t);


  9. t1.start();
  10. try {
  11. Thread.sleep(10);
  12. } catch (InterruptedException e) {

  13. }
  14. t.flag=false;
  15. t2.start();



  16. }

  17. }
复制代码

静态同步函数的锁不就是Class对象吗?怎么会打印出以下结果呢?到底是哪里出错了?是不是和我上一个出现的问题一样的原因?我的以下是打印结果的一部分:
Thread-1....show:4
Thread-1....show:3
Thread-0....code:2
Thread-0....code:1
Thread-0....code:0
Thread-0....code:-1
Thread-0....code:-2
Thread-0....code:-3



作者: 张雪磊    时间: 2012-8-10 11:46
本帖最后由 张雪磊 于 2012-8-10 13:35 编辑

如果需要同步的代码分为两个部分,比如同步代码块的内容抽出了函数,那这两个部分都必须痛不起来
既然是同步就必须保证前提,
同步的前提:1,必须要有两个或者两个以上的线程。
2,必须是多个线程使用同一个锁。

这个我说错了,静态函数的锁确实是Ticket.class



package thread;

public class Ticket3 implements Runnable
{
private static int tick=100;
Object obj=new Object();
boolean flag=true;

@Override
public void run()
{
if(flag)
{
while(true)
{
synchronized(Ticket.class)
{
if(tick>0)
{
try {
Thread.sleep(10);
} catch (InterruptedException e) {
}
}
System.out.println(Thread.currentThread().getName() + "....code:"+ tick--);
}

}
}

else
while(true)
show();
}
public static synchronized void show()
{
if (tick > 0) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {


}
System.out.println(Thread.currentThread().getName() + "....show:"+ tick--);
}
}
}

package thread;
public class ThisLockDemo2 {public static void main(String[] args) {
Ticket3 t=new Ticket3();
Thread t1=new Thread(t);Thread t2=new Thread(t);

t1.start();try {Thread.sleep(10);} catch (InterruptedException e) {
}t.flag=false;t2.start();


}
}



作者: 刘俊佳    时间: 2012-8-14 08:49
张雪磊 发表于 2012-8-10 11:46
如果需要同步的代码分为两个部分,比如同步代码块的内容抽出了函数,那这两个部分都必须痛不起来
既然是同 ...

嗯  我找了半天我代码应该是没错吧  为什么出现负数了
作者: 黄敏    时间: 2012-8-14 08:59
刘俊佳 发表于 2012-8-14 08:49
嗯  我找了半天我代码应该是没错吧  为什么出现负数了

的确没有错,而且你用个的也是两个同步,你的同步方法里么有锁,跟同步代码块的锁不一样导致的
public static synchronized void show()
{
if (tick > 0) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {


}

作者: 刘俊佳    时间: 2012-8-16 09:20
嗯嗯  谢谢楼上各位。问题已经解决




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2