class test {
public synchronized void run() {
//do something
}
}
public synchronized void run() {
//do something
}
}
解决方案 »
- java Map 中如果键相同就让值相加是该怎么实现
- Java按位取反?~2为什么是-3呢?
- running: 的用法是什么?请高人指点。
- 新手求救!请问怎样在类Frame中添加一个字体库??
- swing的UI机制是怎样的?如何定制TableUI?
- 紧急求救:关于大小写的问题
- 請問怎麼樣得到 操作操作系統機器 的名稱
- 这么写可以吗 抱错!!!
- JAVA 用JTextArea写完后保存到文件,怎么实现在文件里面能换行
- 100分求教java 文件操作(如果系统允许,还可再加)
- 如何编译两个java文件?如sphere.java,createspheres.java
- 谁知道在<tag>disp value</tag>里,disp value能否动态改变??(100分)
public class T{
public synchronized void method(){
//......
}
}
相当于
public class T{
public void method(){
synchronized(this){
//......
}
}
}
public class Test{
private MyObject obj = new MyObject();
class MyObject
{
public synchronized find()
{........}
} class ThreadTest extends Thread
{
public void run()
{
obj.find(); //这时,进入函数,这个线程拥有obj的锁
}
}
}
class T的 method()方法),那这个线程就成为 the owner of method() 's monitor(),如果此时有另一个线程,调用这个方法,就必须等待.
2.notify()只能在synchronized中出现,如:
public class T{
public synchronized void method(){
//......
notify()
}
}
作用是释放等待的线程(即用wait()).
3.在很多java教程中有一个"生产者/消费者"的教学程序很说明问题.
http://www.csdn.net/expert/topic/1061/1061597.xml?temp=.3540613
http://www.csdn.net/expert/topic/1059/1059474.xml?temp=.8966638
偶告诉你,有很多地方了,搜索看看吧
{
final static int maxOperations = 20;
static int operatons = 0; WareHouse warehouse = new WareHouse();
Producer pro = new Producer();
Consumer con = new Consumer();
public static void main(String[] args){
ThreadTest test = new ThreadTest();
test.beginTest();
}
public void beginTest()
{
pro.start();
con.start();
}
class Producer extends Thread
{
int operations = 0;
public void run()
{
System.out.println("Producer started");
try
{
while(operations < maxOperations)
warehouse.putIn(++operations);
}
catch (Exception e)
{System.out.println(e); }
}
};
class Consumer extends Thread
{
int operations = 0;
public void run()
{
System.out.println("Consumer started");
try
{
while(operations < maxOperations)
warehouse.fetchOut(++operations);
}
catch (Exception e)
{System.out.println(e); }
}
};class WareHouse
{
int wares;
final int MAX_WARES = 5; public synchronized void putIn(int ops)
{
try
{
while(wares>=MAX_WARES){
System.out.println("Producer:" + ops + "Warehouse full, I have to wait for consumer!");
wait();//block me to let consumer have chance to fetchOut
}
//After consumer done, the consumer will notify me, Is can continue
wares++;//put in one
System.out.println("Producer:" + ops + "I put in one");
//tell consumer that you can fetch because I have just putIn one
notifyAll();
System.out.println("Producer:" + ops + "I tell Consumer that he can fetch");
}
catch (Exception e)
{
System.out.println(e);
}
}
public synchronized void fetchOut(int ops)
{
try
{
while(wares<=0){
System.out.println("Consumer:" + ops + "Warehouse empty, I have to wait for producer!");
wait();//block me to let producer have chance to putIn
}
//After the producer done, producer will notify me, I can continue
wares--;//fetch one
System.out.println("Consumer:" + ops + "I fetch one");
//tell producer that you can putIn because I have just fetch one
notifyAll();
System.out.println("Consumer:" + ops + "I tell Producer that he can put in");
}
catch (Exception e)
{
System.out.println(e);
}
}
};}