topics

全部话题 - 话题: mainthread
(共0页)
b*********n
发帖数: 1258
1
我在MainThread里同时start了两个thread: thread1, thread2
MainThread里等了2sec之后,想要kill thread2
thread2是去obtain一个url 的content,
一般情况thread2在2-3secs之后就return url content,但有时会take up to 10 sec
想要实现在2sec之后,
如果thread2没结束就强行结束,并返回nothing,
如果thread2结束了就返回url content.
用了:thread2.interrupt() 来强行结束thread2;
但是好像没有用呀,thread2还是在运行,并没有end
而且在我连续调用MainThread时,下一次的MainThread返回的url content
是上一次MainThread启动的thread2返回的url content
用的是jre1.6
哪位高手给指点指点吧,谢谢
MainThread:
Thread thread1 = new Thread(new multiThread11());
Thread threa
b*********n
发帖数: 1258
2
【 以下文字转载自 Java 讨论区 】
发信人: babyfacenan (黑土), 信区: Java
标 题: 急问:怎么kill一个thread, thread.interrupte()不好用呀?
发信站: BBS 未名空间站 (Sun Apr 27 15:47:01 2008)
我在MainThread里同时start了两个thread: thread1, thread2
MainThread里等了2sec之后,想要kill thread2
thread2是去obtain一个url 的content,
一般情况thread2在2-3secs之后就return url content,但有时会take up to 10 sec
想要实现在2sec之后,
如果thread2没结束就强行结束,并返回nothing,
如果thread2结束了就返回url content.
用了:thread2.interrupt() 来强行结束thread2;
但是好像没有用呀,thread2还是在运行,并没有end
而且在我连续调用MainThread时,下一次的MainThread返回的url
w*m
发帖数: 1806
3
来自主题: Java版 - newbie question
很简单,就是想测试一下jdbc conneciton
在Linux下,网上照搬一个简单的代码,然后我想用xxxxxxxx/jre/bin/java去运行
请问应该如何做?
javac Conn.java and I get Conn.class
then run Java Conn get
Exception in thread "main" java.lang.NoClassDefFoundError: Conn
at gnu.java.lang.MainThread.run(libgcj.so.7rh)
Caused by: java.lang.ClassNotFoundException: Conn not found in gnu.gcj.
runtime.SystemClassLoader{urls=[file:/apps/oracle/product/10.2.0/db_1/jdbc/
lib/], parent=gnu.gcj.runtime.ExtensionClassLoader{urls=[], parent=null}}
at java.net.URL... 阅读全帖
n*m
发帖数: 23
4
来自主题: Java版 - Can Java thread return a value?

It can be calculated in the new thread. My sample is a typical asynchronous
call. So, you may get invalid value from the call since the new thread may not
finish.
However, there are many ways to get a "correct" value. Here is a sample
public class MyValue {
boolean ready=false;
int realvalue;
public boolean getReady() {
return ready;
}
public void setReady (boolean ready) {
this.ready = ready;
}
}
...
MainThread:
MyValue returnValue = myThread.run();
while
w*r
发帖数: 2421
5
来自主题: Java版 - 来来来,给偶解决一个问题
各位老大, 在java下面没有function pointer,偶现在想做一个thread schedular
也就是一个main thread,下面好多subthread, main thread可以在任何时刻告诉
subthread,你们可以干活了,于是他们就是干活, mainthread等到所有的sub
干完了才可以继续,而且不是每一次不是都要所有的sub干活,比如有时候我要sub 2,3
有时我要sub 3,4
没有fuc pointer,这个东西真的很烦,如果是每次都create thread,这样overhead很大
我现在的做法是sub thread里面是一个infinite loop ,thread对象hold一个object
refreence,然后在loop里面call obj.job(); wait()...
再用一个object控制wait 和notifyall的行为………………
有没有好一些方法?????
B*********h
发帖数: 800
6
来自主题: Java版 - 问个多线程的问题。
you can use a CountDownLatch. it works like this:
1. in the main thread, you say I am going to count down from N.
2. in each of the N threads you want to create and run, count down the latch
at the end of run().
3. the mainthread blocks until the latch count is zero.
go check it out. but you can simply call join() as coconut pointed out.
b*********y
发帖数: 7
7
You should use a flag, say, boolean stopThread = false, it's shared by both
main thread and thread2; thread2 keep polling it in its run() method,once
the flag's status changes, thread2 exits run() and terminates itself.
MainThread is to toggle the flag after 2 seconds.
However, execise with great caution, the toggling and checking the status of
the flag, is to be guarded by synchronization to ensure the mutual
exclusion and inter-thread communication.
Please refer to "Effective Java" 2nd edition
(共0页)