当前位置: 代码迷 >> Java相关 >> 同步语句
  详细解决方案

同步语句

热度:437   发布时间:2007-06-26 23:08:44.0
同步语句

我在考虑线程的同步的时候,对同步语句有点模糊,书上写得也简单。
哪位高手能帮我讲讲,最好再给个例子

搜索更多相关的解决方案: 语句  

----------------解决方案--------------------------------------------------------
这个...怎么说阿
一共三个同步的东东:同步块、同步方法和同步变量

而且还可以细分为:普通同步和静态同步

都不知道你想问什么来着...

----------------解决方案--------------------------------------------------------

有多个线程同时对同一对象的公共属性或方法的访问所造成冲突而须同步,即对象锁!


----------------解决方案--------------------------------------------------------
package thread;
public class Tickets implements Runnable{
private int ticket=100;
public void run(){
yunxing();
}
public void yunxing(){
while(ticket>1){
synchronized(this){
System.out.println(Thread.currentThread().getName()+" Sell the number "+ticket--);
try{
Thread.sleep(10);
}catch(InterruptedException e){
throw new RuntimeException(e);
}
}}
}
public static void main(String[] args){
Tickets t=new Tickets();
Thread t1=new Thread(t);
Thread t2=new Thread(t);
t1.setName("Thread1");
t2.setName("Thread2");
t1.start();
t2.start();

}
}

----------------解决方案--------------------------------------------------------

貌视懂了,谢了哈


----------------解决方案--------------------------------------------------------
  相关解决方案