解决方案 »
- 请问如何强制编译成class?
- 如何判断鼠标的移动方向?如:向左移动,向右移动?
- 使用setLong(A,987654321)向A字段number(9)插入数据,但总是报插入的值过大的错误,何故?
- 一个超难的问题,急盼高手
- 我最近闲得无聊,就把严蔚敏的c数据结构用java写了一遍,收获颇丰啊。(发现好几个错误)
- Java图形界面字符出现乱码
- 我用画布(Canvas类)接受MouseEvent事件,但我又想这个事件的getX()方法返回的坐标是以窗口的原点为原点,该怎么办?
- java UTF-8
- 小弟初学,请指教,我对数据封装不太明白!
- ArrayList 问题
- 关于 php ,java 取yahoo site ,yahoo link, google link ,google site
- 获得一个不相同的编号,不重复
可能是10太小了 你试着改成1000看看
class ThreadDemo implements Runnable
{
private int ticket=10;
public synchronized void fun()
{
if(this.ticket>0)
{
System.out.println(Thread.currentThread().getName()+"卖票:----->"+this.ticket--);
try
{
//延迟操作,产生不同步的后果
Thread.sleep(100);
}
catch (Exception e)
{
}
// System.out.println(Thread.currentThread().getName()+"卖票:----->"+this.ticket--);
}
}
public void run()
{
while(ticket>0)
{
this.fun();
}
}
}
搞定了,将注释地方的代码放到红色字体位置,就OK了,但是不知道什么原因。
class ThreadDemo implements Runnable
{
private int ticket=10;
public synchronized void fun()
{
if(this.ticket>0)
{
System.out.println(Thread.currentThread().getName()+"卖票:----->"+this.ticket--);
try
{
//延迟操作,产生不同步的后果
Thread.sleep(100);
}
catch (Exception e)
{
}
// System.out.println(Thread.currentThread().getName()+"卖票:----->"+this.ticket--);
}
}
public void run()
{
while(ticket>0)
{
this.fun();
}
}
}
搞定了,将注释地方的代码放到红色字体位置,就OK了,但是不知道什么原因
sleep不会释放锁,去掉那行代码应该也没关系吧
* Causes the currently executing thread to sleep (temporarily cease
* execution) for the specified number of milliseconds. The thread
* does not lose ownership of any monitors.
*
* @param millis the length of time to sleep in milliseconds.
* @exception InterruptedException if another thread has interrupted
* the current thread. The <i>interrupted status</i> of the
* current thread is cleared when this exception is thrown.
* @see java.lang.Object#notify()
*/
public static native void sleep(long millis) throws InterruptedException;不知道8楼用的是哪个版本的java啊?
微秒?Are you sure?....