I have the same question as this.
--------------------------------------------
I am confused a bit about wait and notify/notifyall.
I know there is a lock for every java object. I know wait will release the
lock for other thread. How about notify/notifyall? Does notify/notifyall
realse the lock it is holding for other thread?
-------------------------------------------------
There are 2 different answers for it on this link http://stackoverflow.com/questions/5999193/java-wait-notify-not
Could an... 阅读全帖
Since you have the code, you should be able to run it and know the answers
to your questions. Seems that you are testing others :)
A will wait until B finishes the loop.
B releases the lock after the sync block.
To answer your question 3, you need to understandthe thread states. When A calls
wait(), A is put in the WAITING state. It will wait until a notify() or
notifyAll(). When that happens, A is NOT put int the RUNNABLE state, but
BLOCKED state. A waits for the lock in the BLOCKED state. When... 阅读全帖
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;
public class H2O {
//using blocking queue, so it's easy to just have 2 h and 1 o at most,
//other threads will be blocked.
ArrayBlockingQueue
继续consumer和producer的例子
package jc;
import java.util.*;
//what about there are two things to be synchronized?
public class ProCon2nd {
// a class object to store the strings
static List myListOne = new ArrayList();
static List myListTwo = new ArrayList();
// this is the thing getting synchronized
// it is static as it is the same for all instances
static Object myLock = new Object();
public void produce(){
... 阅读全帖
public class BQueue {
private Queue q = new LinkedList();
private int limit;
public BQueue(int limit) {
this.limit = limit;
}
public synchronized void put (T t) throws InterruptedException {
while (isFull()) {
wait();
}
boolean e = isEmpty();
q.add(t);
if (e)
notifyAll();
}
public synchronized T get () throws InterruptedException {
while (isEmpty()) {
wait();
}... 阅读全帖
int counter[2]; // count[0] count of O; count[1] count of H
int toRunH ;
int toRunO ;
void H( ){
sync(counter){
if(counter[0]>=1 && count[1]==2){
toRunH = 2;toRunO = 1;
counter.notifyAll();
int counter[2]; // count[0] count of O; count[1] count of H
int toRunH ;
int toRunO ;
void H( ){
sync(counter){
if(counter[0]>=1 && count[1]==2){
toRunH = 2;toRunO = 1;
counter.notifyAll();
试着解第一题:
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class DeepIteratorImpl implements Iterator {
//use a flat list to store all elements
private int currIndex;
private ArrayList flatC;
// Constructor
public DeepIteratorImpl (Collection> c) {
currIndex=-1;
flatC=new ArrayList();
getElement(c, flatC);
}
//get all the elements from the collection
private void getElement(C... 阅读全帖
经典的consumer和producer的例子
package jc;
import java.util.*;
public class ProCon {
// a class object to store the strings
static List myListOne = new ArrayList();
public void produce(){
synchronized(myListOne){
myListOne.add("firstNameOne");
// notice all waiting threads to stop waiting
myListOne.notifyAll();
System.out.println("Adding finished and the current myListOne.
size(): " + myListOne.size());
... 阅读全帖
To be frankly I am afraid that you got a wrong solution. The problem is the
code here:
Object value = // initialization;
theWrap.putValue(value);
There are no memory barrier between initialization and the writes that set
the value before your synchronized notifyAll(). Image a thread read the
value before notifyAll(). It may detect the value is non
-null, but it actually may be uninitialized.
Your solution is essentially a double check pattern that tries to use notify
to solve the problem, not to
import java.util.*;
import java.util.concurrent.locks.*;
public class PC_unsyn
{
public static void main(String[] args)
{
IntBuffer b = new IntBuffer();
Producer p = new Producer(b);
Consumer c = new Consumer(b);
p.setName("Producer");
c.setName("Consumer");
p.start();
c.start();
}
}
class IntBuffer
{
private int index;
private int[] buffer = new int[8];
private Lock bufferLock = new ReentrantLock();
private Cond... 阅读全帖
1. Difference between notify and notifyAll
2. Difference between sync a static method and non-static method
3. Difference between join and yield
4. How to stop a thread?
5. Talking about concurrent package
6. What is ThreadLocal class?
恕我弩钝, 请问这题和career cup 上的那个题目有啥区别啊?
class F{
public A(); public B(); public C();
}
Can you design a mechanism to make sure that B is executed after A, C is
executed after B?
And the solution is using two semaphore synchronize between A&B, B&C. I
think the question is talking about multithreads share the same objects.
在Java里就是用wait and notifyall.
谢谢
public class H2O {
public static class ThreadHolder {
Thread thread;
ThreadHolder(Thread thread) {
this.thread = thread;
}
}
protected LinkedList hlist = new LinkedList();
protected LinkedList olist = new LinkedList();
public boolean h() {
ThreadHolder current = new ThreadHolder(Thread.currentThread());
synchronized (current) {
ThreadHolder[] threadsToWakeUp = null;
// updating the queues requires a global lock;
sy... 阅读全帖
public class H2O {
public static class ThreadHolder {
Thread thread;
ThreadHolder(Thread thread) {
this.thread = thread;
}
}
protected LinkedList hlist = new LinkedList();
protected LinkedList olist = new LinkedList();
public boolean h() {
ThreadHolder current = new ThreadHolder(Thread.currentThread());
synchronized (current) {
ThreadHolder[] threadsToWakeUp = null;
// updating the queues requires a global lock;
sy... 阅读全帖
send 那个APi设计
思路1加messsage queue
但是这个不是api 设计级别的问题
另外一个思路就是解决backpressure问题。
Back-pressure
A means of flow-control, a way for consumers of data to notify a producer
about their current availability, effectively slowing down the upstream
source to match their consumption speeds. In the context of Akka Streams
back-pressure is always understood as non-blocking and asynchronous
简单的说,send Query过去,返回还能处理多少条数据。
具体实现。
如果是actor的话,好办的多, 可以push, 可以pull.可以定义消息,slow down or
speed up.
可以加LB,加多个processor.等等
你要... 阅读全帖
send 那个APi设计
思路1加messsage queue
但是这个不是api 设计级别的问题
另外一个思路就是解决backpressure问题。
Back-pressure
A means of flow-control, a way for consumers of data to notify a producer
about their current availability, effectively slowing down the upstream
source to match their consumption speeds. In the context of Akka Streams
back-pressure is always understood as non-blocking and asynchronous
简单的说,send Query过去,返回还能处理多少条数据。
具体实现。
如果是actor的话,好办的多, 可以push, 可以pull.可以定义消息,slow down or
speed up.
可以加LB,加多个processor.等等
你要... 阅读全帖
There are still several issues left in the code:
1.why is it necessary to create a new thread every time to
make a new connection?
2.the free() method relies on one assumption that
the implementation class
of Connection implement equals() properly. Or busy connections
won't be released correctly.
3.the notifyAll() call in the middle of getConnection
doesn't make much sense, because the caller thread will keep
the lock and call getConnection again, while all other threads
wakened up will simply h