本帖最后由 李凤鸣 于 2012-12-7 01:43 编辑
- class Ticket4 implements Runnable
- {
- private static int tick=100;
- Object object=new Object();
- boolean flag=true;
- public void run()
- {
- if (flag) {
- while (true) {
- synchronized (this){
- if (tick>0) {
- try {
- Thread.sleep(10);
- } catch (Exception e) {
- // TODO: handle exception
- }
- 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 (Exception e) {
- // TODO: handle exception
- }
- System.out.println(Thread.currentThread().getName()+"..show()..."+tick--);
- }
- }
- }
- public class StaticMethodDemo {
- public static void main(String[] args) {
- Ticket4 ticket4=new Ticket4();
- Thread t1=new Thread(ticket4);
- Thread t2=new Thread(ticket4);
- t1.start();
- try {
- Thread.sleep(10);
- } catch (Exception e) {
- // TODO: handle exception
- }
- ticket4.flag=false;
- t2.start();
- }
- }
复制代码 使用锁时,静态方法中也不可以定义this
可我用this出的结果跟老师的不一样
老师的最后会打印出零
可我的运行几遍之后都是正常的啊,到底哪里有问题了
结果:
Thread-0..code :100
Thread-1..show()...99
Thread-1..show()...98
Thread-1..show()...97
Thread-1..show()...96
Thread-0..code :95
Thread-0..code :94
Thread-0..code :93
Thread-0..code :92
Thread-0..code :91
Thread-0..code :90
Thread-0..code :89
Thread-0..code :88
Thread-0..code :87
Thread-0..code :86
Thread-0..code :85
Thread-0..code :84
Thread-0..code :83
Thread-0..code :82
Thread-0..code :81
Thread-0..code :80
Thread-0..code :79
Thread-0..code :78
Thread-0..code :77
Thread-0..code :76
Thread-0..code :75
Thread-0..code :74
Thread-0..code :73
Thread-0..code :72
Thread-0..code :71
Thread-0..code :70
Thread-0..code :69
Thread-0..code :68
Thread-0..code :67
Thread-0..code :66
Thread-0..code :65
Thread-0..code :64
Thread-0..code :63
Thread-0..code :62
Thread-0..code :61
Thread-0..code :60
Thread-0..code :59
Thread-0..code :58
Thread-0..code :57
Thread-0..code :56
Thread-0..code :55
Thread-0..code :54
Thread-0..code :53
Thread-0..code :52
Thread-0..code :51
Thread-0..code :50
Thread-0..code :49
Thread-0..code :48
Thread-0..code :47
Thread-0..code :46
Thread-0..code :45
Thread-0..code :44
Thread-0..code :43
Thread-0..code :42
Thread-0..code :41
Thread-0..code :40
Thread-0..code :39
Thread-0..code :38
Thread-0..code :37
Thread-0..code :36
Thread-0..code :35
Thread-0..code :34
Thread-0..code :33
Thread-0..code :32
Thread-0..code :31
Thread-0..code :30
Thread-0..code :29
Thread-0..code :28
Thread-0..code :27
Thread-0..code :26
Thread-0..code :25
Thread-0..code :24
Thread-0..code :23
Thread-0..code :22
Thread-0..code :21
Thread-0..code :20
Thread-0..code :19
Thread-0..code :18
Thread-0..code :17
Thread-0..code :16
Thread-0..code :15
Thread-0..code :14
Thread-0..code :13
Thread-0..code :12
Thread-0..code :11
Thread-0..code :10
Thread-0..code :9
Thread-0..code :8
Thread-0..code :7
Thread-0..code :6
Thread-0..code :5
Thread-0..code :4
Thread-0..code :3
Thread-0..code :2
Thread-0..code :1
我自己找到原因了{:soso_e106:}
|