- class Ticket implements Runnable
- {
- Object obj = new Object();
- private int i=100;
- public void run()//要执行的代码;
- {
- while(true)
- {
- synchronized(obj)
- { if(i>0)
- {
- try
- {
- Thread.sleep(10);
- }
- catch(Exception e)
- {
-
- }
-
- System.out.println(Thread.currentThread().getName()+"---geshu ---"+i--);
- }
- }
-
- }
- }
- }
- class ThreadTest
- {
- public static void main(String[] args)
- {
- Ticket t = new Ticket();//
- Thread t1 = new Thread(t);//此处为创建线程对象。
- Thread t2 = new Thread(t);
- Thread t3 = new Thread(t);
- Thread t4 = new Thread(t);
- t1.start();
- t2.start();
- t3.start();
- t4.start();
-
- }
- }
复制代码
1.我这样写有什么弊端么,,,
2.怎么区分是不是共享数据{:3_55:}
3.还有同步的前提怎么理解。
4.同步代码块和同步函数有很么区别。。。
求大神告知,,,,,,,:handshake{:3_64:} |
|