m******t 发帖数: 2416 | 1
Hmmm what I was trying to say is to use a temp file to "flag"
the locking status would have the same drawback as to use a global
variable to check whether a thread is still alive. That is,
if the program currently holding the lock fails to delete the
"flag file" before it exits, there will be deadlock. |
|
g**y 发帖数: 983 | 2 呵呵
所以我就是想知道是不是所有UNIX OS都支持,
WINDOWS是否是真的THREAD调度的我倒不在乎
不然我就得做成多进程的来保证利用多CPU了
against |
|
m******t 发帖数: 2416 | 3
Either I misunderstood you, or you are saying multi-thread doesn't
utilize multi processor, which doesn't sound right to me. |
|
n*****k 发帖数: 123 | 4
the
on
on
only
x++
y
I am pretty sure the key is wrong.
You can varify that by either adding
if (x! = y)
System.out.println("x = " + x + " y = " + y);
insider the run method.
or Thread.currentThread().sleep(100); between x ++ and y++,
You will see that x and y will have difffernt value.
Hope this will help. |
|
m******t 发帖数: 2416 | 5
I believe it has more to do with the facts that both threads
are started almost immediately back to back, that they run
on exactly the same code, and that there probably wasn't a lot
of other things running when this test program was executed.
If you insert a random sleep between
x++ and y++, it'll make some difference. |
|
J*****a 发帖数: 12 | 6 if you insert some random sleeps, i should be different, but it's not
the same env.
the orginal is the circle time is what jvm excute x++ and y++, and
the later should plus a random paremeter.
in my remember, one of win and linux's thread mode is 抢占式, so ... |
|
y***i 发帖数: 279 | 7 This is just way too much for a simple problem. Just launch a new thread
for computing as user finishes inputing, which dies after it gets and
saves the result. This is much similar to a swing GUI, and user
clicks go. |
|
n*****k 发帖数: 123 | 8
It is java.lang.reflect, buddy.
the
二
thread)平时处于idel。
后 |
|
g****n 发帖数: 18 | 9 Hello everyone,
I have a question about Java thread sync. Following is part of the code that I
am having trouble with,
while (listening)
{
System.out.println("Waiting for a client...");
new MultiThread(group, serverSocket.accept(), h).start();
trackTime = h.getTime();
System.out.println("Track time " + trackTime);
System.out.println("Load: " + group.activeCount());
}
In it, h is a structure with public function getTime(). The MultiThread will
m |
|
c****e 发帖数: 90 | 10 Try the following code and you'll see why.
For primitive types, java just copy the value
(instead of passing the reference of that object)
when passing parameters.
class t {
private static void modify(int val)
{
val += 1;
System.out.println("Value of integer inside: "+val);
}
public static void main(String []args){
int t = 0;
modify(t);
System.out.println("Value of integer outside: "+t);
}
}
number
thread
is |
|
g****n 发帖数: 18 | 11 我有一个client和server编程上的问题,在程序中有两个client
socket,比如说socket_1和socket_2在client中,同样在server一边有两个对应的socket
,server_socket_1和server_socket_2,
现在要求socket_1和server_socket_1通信,用single
thread,socket_2和server_socket_2通信,用multithread,做不同的事情,问题是怎么
样让这两个通信并行工作(这两个通信不是synchronized) ?
非常感谢! |
|
g****n 发帖数: 18 | 12 Hello everyone,
Does anyone know in Java can a thread return a value, like a function can do?
Thanks! |
|
n*m 发帖数: 23 | 13
He just wants a thread can return a value, or say, needs a sort of
asynchronous call. Your function is mutex operation. I don't think they are
the same thing. |
|
xt 发帖数: 17532 | 14
With all due respect, I do not often call this kind of
code "program". You are wasting your CPU time doing nothing.
You could put a Thread.sleep(100) in the while loop.
That could be somewhat acceptable, especially under
such circumstances that the program is not mission critical
i.e. requirement on programming correctness is not as high. |
|
xt 发帖数: 17532 | 15
一点都不古怪.我整天写这玩意.见Thread的constructor. |
|
c*******a 发帖数: 29 | 16 在单cpu上面,做模拟,想N个agent
都是independent的,然后他们自己决定自己的策略,
不知道开N个thread能不能做到?
还是单cpu就没法做到independent? |
|
e***g 发帖数: 158 | 17 you want N cpu? :)
it's fine, just use threads. |
|
o*******a 发帖数: 46 | 18 class Reader extends Thread {
Calculator c;
public Reader(Calculator calc){
c = calc;
}
public void run(){
synchronized(c){
try{
System.out.println("Waiting for calculation...");
c.wait();
}
catch(InterruptedException e){
}
System.out.println("Total is :" +c.total);
}
}
public static void main(String[] args){
Calculator calculator = new Calculator();
new Reader(calculator).start();
new Reader(calculator).start();
new Reader(calculator).start();
calcula |
|
g*******e 发帖数: 14 | 19 I guess that Thread object's notifyAll method gets invoked internally when
it exits.
Although locks can be used on any Object theoretically. But the application
would better not to use it if the platform internally uses the lock on
this object.
Correct me if I am wrong. |
|
m******t 发帖数: 2416 | 20
I'd always implement Runnable. It provides greater flexibility and less
coupling with the threading api at the mere price of one extra line of code. |
|
w**e 发帖数: 37 | 21 我需要运行1000000个threads,内存不够。我用 java -XX:+PrintGCDetails 看了一下
diagnostic output:
[GC [DefNew: 493K->64K(576K), 0.0076700 secs] 2413K->2412K(3776K), 0.0077730
secs]
[GC [DefNew: 575K->64K(576K), 0.0029430 secs] 2924K->2729K(3776K), 0.0030290
secs]
[GC [DefNew: 575K->63K(576K), 0.0024980 secs] 3241K->3041K(3776K), 0.0025810
secs]
那位大虾给分析分析吧? 多谢了!!! |
|
m******t 发帖数: 2416 | 22
Forget about memory, 1 mln threads will for sure kill your CPU(s).
Unless, of course, if you are running it on some super uber NSA computer. |
|
n******t 发帖数: 4406 | 23 那更没必要100000个threads.
模拟时钟又没必要等于计算机时钟。 |
|
I*******e 发帖数: 1879 | 24 ☆─────────────────────────────────────☆
maplerstone (木鱼豆腐) 于 (Tue Jul 17 01:18:31 2007) 提到:
有一个负责调度的server程序, 根据需要会随时创建新的thread来执行子任务
现在我希望这些子线程结束自己的任务后会实时通知主进程,请问可以用什么方式通知
?
☆─────────────────────────────────────☆
Amao (蹭饭 打牌 八卦) 于 (Tue Jul 17 02:02:33 2007) 提到:
what's your scheduling model?
☆─────────────────────────────────────☆
maplerstone (木鱼豆腐) 于 (Tue Jul 17 09:52:21 2007) 提到:
简化来说,主程序以时间片轮换的方式调度n个线程;一个时间只有一个线程在运行
1、如果线程A用完了分配给它的时间片,主程序会暂时中止它,继续执行队列中下一个
线程
2、如果线程B在时间片用完之前就完成 |
|
|
g*****n 发帖数: 239 | 26 有什么方法可以检查一个thread是否已经被suspend了?
或者有什么suspend/resume的替代? |
|
g*****g 发帖数: 34805 | 27 You either create logger per thread, or maybe extend SMTPAppender and
filter the log the way you want.
warning |
|
A**o 发帖数: 1550 | 28 question: how do you capture error and remembers the previous log event?
or how many emails you receive in the single thread mode in your example?
warning |
|
l*****b 发帖数: 82 | 29 Hi,Amao, I use SMTPAppender. It has a buffer setting to record specific
number of log events. My setting in single thread mode is 10. When error
occurs, one email is send out with 10 events. |
|
g*****g 发帖数: 34805 | 30 There's only one standard input and output I believe.
It's easy to log to different files but I don't know
how to open multiple standard input.
You can always force the input or output to use an identifier.
threads都
out。
的话 |
|
|
h*****0 发帖数: 4889 | 32 这种方法确实thread safe,我的意思是inner class的private成员是不是不可以直接
得到?能编译通过吗? |
|
g*****g 发帖数: 34805 | 33 I believe when you declare
private static final Singleton INSTANCE = new Singleton();
the instance is created when you load the class, not when you
call getInstance, so it's not lazy initialization and it should
be thread safe. |
|
A**o 发帖数: 1550 | 34 talking about thread safe, what if different classloaders in the same jvm
are loading the same single class multiple times? |
|
s*******e 发帖数: 174 | 35 Why? I thought it is the most common thread safe solution. |
|
s***c 发帖数: 1926 | 36 exactly.这是唯一的真正thread safe的方法。 |
|
g*****g 发帖数: 34805 | 37 网站通常需要同步的地方还是比较少的,基于socket直接通讯
的server通常就要处理thread pool这些东西,碰到的问题会
比较多。 |
|
g*****g 发帖数: 34805 | 38 That's the common case in real world application unfortunately.
Many times you don't have enough log to know what's going on,
you first try to guess and see if you can reproduce, if all
fail, add more logging so hopefully you can figure out next
time.
If you suspect something and want to prove it, you can use
HtmlUnit to simulate http requests. It's pretty easy to use,
much better than JMeter, the latter is more of a performance
testing tool.
Another way to debug multiple thread issue in product |
|
y***u 发帖数: 5243 | 39 multi-threading只是一个很小的方面啊,做server第一个要考虑的就是
multithreading的问题。不管是不是webbased的 |
|
u****s 发帖数: 2186 | 40 try
Thread.getCurrentThread().getId() |
|
I**A 发帖数: 2345 | 41 【 以下文字转载自 JobHunting 讨论区 】
发信人: IAYA (你是我的天使), 信区: JobHunting
标 题: multi-threading guru们
发信站: BBS 未名空间站 (Thu Nov 11 12:25:01 2010, 美东)
java里
Class C{
method 1{}
method 2{}
}
How can we guarantee that method 1 can only be called after method 2 is
called and finished?
Thanks a lot~ |
|
l**********n 发帖数: 8443 | 42 thread1.join(); // in thread1 call method1
// in main thread call method2 |
|
|
l**********n 发帖数: 8443 | 44 if only one thread needs be waken up, then use notify, otherwise use
notifyall. |
|
m****r 发帖数: 6639 | 45 i re-write job2 like this:
class job2 {
run() {
synchronize (obj) {
obj.method2;
obj.set method2 done;
if (need to wake up only one thread) {
obj.notify();
} else {
obj.notifyAll();
}
}
} |
|
L*******r 发帖数: 8961 | 46 没意思。一件事一件事地办不就完了?本来就只有一个CPU,
非要Multi-Threading?岂不是自欺欺人? |
|
g*****g 发帖数: 34805 | 47 It's a bad design to use 2 theads here. The main problem though,
is not that you can't sync between 2 theads, but that upload file
takes a long time while a DB transaction typically is short, it doesn't
make sense to wait until upload is finished to commit the transaction.
Of course if you can still use a compensation transaction to undo
previous transaction. But I don't see why you can't just put them
all in one thread, do upload first, then do db transaction, if upload
fails, throw an exceptio... 阅读全帖 |
|
s********r 发帖数: 176 | 48 Thanks goodbug for your idea.
I completely agree with you that using one thread to handle the two
processes is the right design.
For some whatever reason, I have to follow the existing bad implementation :
(
Have good Friday. |
|
g*****g 发帖数: 34805 | 49 If you don't commit immediately, you may run out of DB threads while
waiting for file upload. It's worse than compensation transaction.
finish, |
|
g*****g 发帖数: 34805 | 50 这个并不是问题,但如果你应该查一查有没有死锁。
另外如果性能有问题应该调thread pool. |
|