多线程重写run方法出错
本帖最后由 qwuiop789 于 2015-5-1 07:43 编辑package com.dd.qn;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
class ticketsimplements Runnable{
private int tickets=0;
private Lock lock = new ReentrantLock();
@Override
publicvoid run() {
Boolean flag =true;
while(flag){
if(tickets<100){
tickets=tickets+1;
System.out.println(Thread.currentThread().getName()+"卖出第"+tickets+"张票");
}
else{
flag =false;
}
lock.unlock();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class beijing2008{
public static void main(String[] args) {
tickets to=new tickets();
Thread w1=new Thread(to);
w1.setName("售票点1");
w1.start();
Thread w2=new Thread(to);
w2.setName("售票点2");
w2.start();
Thread w3=new Thread(to);
w3.setName("售票点3");
w3.start();
Thread w4=new Thread(to);
w4.setName("售票点4");
w4.start();
Thread w5=new Thread(to);
w5.setName("售票点5");
w5.start();
}
}
把多个类写在同一个*.class类文件中,这种写法叫什么写法?【看上面蓝色部分代码】。
售票点1卖出第1张票Exception in thread "售票点3" java.lang.IllegalMonitorStateException
售票点3卖出第3张票
售票点2卖出第2张票
售票点4卖出第5张票
售票点5卖出第4张票
at java.util.concurrent.locks.ReentrantLock$Sync.tryRelease(ReentrantLock.java:127)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.release(AbstractQueuedSynchronizer.java:1175)
at java.util.concurrent.locks.ReentrantLock.unlock(ReentrantLock.java:431)
at com.dd.qn.tickets.run(beijing2008.java:21)
at java.lang.Thread.run(Thread.java:619)
Exception in thread "售票点1" java.lang.IllegalMonitorStateException
at java.util.concurrent.locks.ReentrantLock$Sync.tryRelease(ReentrantLock.java:127)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.release(AbstractQueuedSynchronizer.java:1175)
at java.util.concurrent.locks.ReentrantLock.unlock(ReentrantLock.java:431)
at com.dd.qn.tickets.run(beijing2008.java:21)
at java.lang.Thread.run(Thread.java:619)
Exception in thread "售票点5" java.lang.IllegalMonitorStateException
at java.util.concurrent.locks.ReentrantLock$Sync.tryRelease(ReentrantLock.java:127)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.release(AbstractQueuedSynchronizer.java:1175)
at java.util.concurrent.locks.ReentrantLock.unlock(ReentrantLock.java:431)
at com.dd.qn.tickets.run(beijing2008.java:21)
at java.lang.Thread.run(Thread.java:619)
Exception in thread "售票点4" java.lang.IllegalMonitorStateException
at java.util.concurrent.locks.ReentrantLock$Sync.tryRelease(ReentrantLock.java:127)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.release(AbstractQueuedSynchronizer.java:1175)
at java.util.concurrent.locks.ReentrantLock.unlock(ReentrantLock.java:431)
at com.dd.qn.tickets.run(beijing2008.java:21)
at java.lang.Thread.run(Thread.java:619)
Exception in thread "售票点2" java.lang.IllegalMonitorStateException
at java.util.concurrent.locks.ReentrantLock$Sync.tryRelease(ReentrantLock.java:127)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.release(AbstractQueuedSynchronizer.java:1175)
at java.util.concurrent.locks.ReentrantLock.unlock(ReentrantLock.java:431)
at com.dd.qn.tickets.run(beijing2008.java:21)
at java.lang.Thread.run(Thread.java:619)
Java的么? 速皮尔 发表于 2015-4-30 21:34
Java的么?
对啊。我解决问题了,线程同步时使用对象锁:忘了加锁,lock.lock。但有解锁,lock.unlock。 if(tickets<100){,后面加锁:lock.lock。
页:
[1]