wait

Are Java wait(), notify() 's implementation significantly different from locks?

淺唱寂寞╮ 提交于 2019-12-05 03:40:37
Out of curiosity, when Java implements wait() and notify() methods, are they really just using locks? i.e., wait() acquires a mutex, notify() release a mutex, notifyAll() releases all mutexes (in the same object, of course)? Other than being less cumbersome than using locks, are there other advantages of using wait() and notify()? [EDIT] I realized what I confused myself about after Brian's comments: wait doesn't lock, it releases the lock and passes it to someone else who's waiting on a synchronized statement for the mutex, then waits to be notified by someone else who has the lock and calls

I used wait(&status) and the value of status is 256, why?

我怕爱的太早我们不能终老 提交于 2019-12-05 03:18:41
I have this line in my code : t = wait(&status); When the child process works, the value of status is 0, well. But why does it return 256 when it doesn't work? And why changing the value of the argument given to exit in the child process when there is an error doesn't change anything (exit(2) instead of exit(1) for example)? Thank you Edit : I'm on linux, and I compiled with GCC. I defined status like this int status; t = wait(&status); Given code like this... int main(int argc, char **argv) { pid_t pid; int res; pid = fork(); if (pid == 0) { printf("child\n"); exit(1); } pid = wait(&res);

Best way to wait in Java

落爺英雄遲暮 提交于 2019-12-05 01:54:25
问题 I have an app that needs to wait for some unknown amount of time. It must wait until several data fields are finished being populated by a server. The server's API provides me a way to request data, easy enough... The server's API also provides a way to receive my data back, one field at a time. It does not tell me when all of the fields are finished being populated. What is the most efficient way to wait until my request is finished being processed by the server? Here's some pseudocode:

go语言:WaitGourp使用

笑着哭i 提交于 2019-12-04 23:46:15
在学习go语言时,用到了“sync”包中的WaitGourp结构,来达到控制任务的完成。 由于WaitGroup是结构,所有需要通过 sync.WaitGroup{} 来创建 很类似与 Shoper/JAVA并发库之倒记时锁存器CountDownLatch 中的CountDownLatch类。 Add(),Done(),Wait()。其中Done()是Add(-1)的别名。简单的来说,使用Add()添加计数,Done()减掉一个计数,计数不为0, 阻塞Wait()的运行。 package main import ( "fmt" "runtime" "sync" ) func main() { runtime.GOMAXPROCS(runtime.NumCPU()) waitGourp := sync.WaitGroup{} waitGourp.Add(10) for i := 0; i < 10; i++ { go Go(&waitGourp, i) } waitGourp.Wait() } func Go(wg *sync.WaitGroup, index int) { sum := 0 for i := 0; i < 100000000; i++ { sum += i } fmt.Println(index, sum) wg.Done() } PS:由于之前用的是java

sync.WaitGroup的错误用法与纠正

不羁的心 提交于 2019-12-04 23:37:42
sync.WaitGroup的错误用法与纠正 本文主要是记录在学习golang语言过程中错误使用sync.WaitGroup以及指针引起的报错,并且纠正过程。 1.错误使用场景 main包的实现内容: package main import ( "fmt" "pressure/game" "sync" ) var waitGroup sync.WaitGroup //定义一个同步等待的组 func main(){ waitGroup.Add (1 ) //添加一个计数 go game.ConnSocket(serverAddr, waitGroup) //调用其他包的方法执行任务 waitGroup.Wait() //阻塞直到所有任务完成 fmt.Println( "main DONE!!!" ) } 如下是game包的内容: package game import ( "fmt" "net" "strconv" "strings" "sync" ) var gameWait sync.WaitGroup func ConnSocket(serverAddr string , wait sync.WaitGroup) { var err error Conn, err = net.Dial( "tcp" , serverAddr) if err != nil { fmt

go的WaitGroup使用及源码分析

走远了吗. 提交于 2019-12-04 23:36:47
源码使用的是1.9版本;sync 包里的WaitGroup主要用于线程的同步;计数主线程创建的子线程(WaitGoup.Add(i));调用清除标记方法(WaitGroup.Done());使用WaitGroup.Wait()来阻塞,直到所有子线程(标记=0)执行完毕。 例子: package main import ( "sync" "fmt" ) func main(){ var swg sync.WaitGroup for i: =0 ;i <3 ;i++{ //增加一个计数器 swg.Add (1 ) go func (wg *sync.WaitGroup,mark int ){ //减去计数器 defer wg.Done() //等价于 wg.Add(-1) fmt.Printf( "%d goroutine finish \n" ,mark) }(&swg,i) } //等待所有go程结束 swg.Wait() } 结果: 2 goroutine finish 1 goroutine finish 0 goroutine finish 注意!如果将代码改成下面这样(子线程函数,传入的参数是waitgroup的值拷贝),会出现什么情况呢? func main(){ var swg sync.WaitGroup for i: =0 ;i <3 ;i++{ swg.Add

Is a notify signalled on thread finish? Why does this code sample work?

允我心安 提交于 2019-12-04 23:10:07
I am looking in some puzzles for threads and I can't figure out why the following consistently prints 999999 : class Job extends Thread { private Integer number = 0; public void run() { for (int i = 1; i < 1000000; i++) { number++; } } public Integer getNumber() { return number; } } public class Test { public static void main(String[] args) throws InterruptedException { Job thread = new Job(); thread.start(); synchronized (thread) { thread.wait(); } System.out.println(thread.getNumber()); } } There is no notify on the same lock (and spurious wakeup seem to be ignored). If a thread finishes

Delay or Wait-For Statement

独自空忆成欢 提交于 2019-12-04 22:35:10
I have a 500,000 line SQL script: update users set region_id = 9814746 where id = 101 and region_id is null; update users set region_id = 9814731 where id = 102 and region_id is null; update users set region_id = 3470676 where id = 103 and region_id is null; I want to INSERT a delay of 10 seconds every 50 lines. Does pgsql have a waitfor statement like t-sql . Thanks. Does pgsql have a waitfor statement like t-sql. Yes, pg_sleep : pg=> SELECT pg_sleep(10); pg_sleep ---------- (1 row) You could call the pg_sleep function with the PERFORM statement since we don't care about returning values:

IllegalMonitorStateException notify() and wait() [duplicate]

时光总嘲笑我的痴心妄想 提交于 2019-12-04 22:33:21
问题 This question already has answers here : Java Wait and Notify: IllegalMonitorStateException (2 answers) Closed last year . I have a problem. When I use notify() in synchronized block I have IllegalMonitorStateException. Can anyone help me to solve this problem? I have to do that, one thread will send to second thread char, then this thread has to wait and second thread print this char. After that second thread wait, and first one again send next char Main.java: import java.util.logging.Level;

Alternatives to using Thread.Sleep for waiting

拟墨画扇 提交于 2019-12-04 16:03:52
问题 Firstly I am not asking the same question as C# - Alternative to Thread.Sleep?, or Alternative to Thread.Sleep in C#?. I don't think I am using it incorrectly and need a genuine alternative for specific situations. During a code analysis run I saw a surprising violation coming up: Usage of Thread.Sleep() is a sign of flawed design. This violation leads to Peter Richie's article on why exactly this constitutes bad design. We all know thread creation is expensive and blocking in threads means