如何让客户在函数中休眠?
How to make client sleep in the function?
我在客户端调用的函数上有一个线程池..只让 (n) 个客户端执行此函数 upload()
,其他客户端等待..我试图调用 sleep()
该功能的实现,但它没有工作...
注意:我这样做是为了有时间查看其他客户端不执行该功能,而有 (n) 个客户端执行它...
我需要快速帮助..
服务器代码:
public class Server extends UnicastRemoteObject implements ExcutorInterface
{
public Server()throws RemoteException
{
System.out.println("Server is in listening mode");
}
public static void main(String arg[]) throws InterruptedException
{
try{
LocateRegistry.createRegistry(1234);
Server p=new Server();
Naming.bind("//127.0.0.1:1234/obj",p);
}catch(Exception e)
{
System.out.println("Exception occurred : "+e.getMessage());
}
}
@Override
public void executeJob() throws RemoteException {
System.out.println("Inside executeJob...");
doJob a=new doJob("req_id","usrname","pwd");
ExecutorService threadExecutor = Executors.newFixedThreadPool(2);
threadExecutor.execute(a);
threadExecutor.shutdown();
}
}
doJob代码:
public class doJob implements Runnable {
String request_id="", usrnamee="", pswd="";
public static int i = 1;
public doJob(String request_id,String usrnamee,String pswd) {
this.request_id=request_id;
this.usrnamee=usrnamee;
this.pswd=pswd;
}
public void upload() throws InterruptedException, IOException {
Thread.sleep(1000*15);
}
public void run() {
upload();
}
}
我在客户端调用 executeJob();
希望Thread.sleep()能帮您解决。
你也可以使用 wait().
为了详细说明我的评论,您应该将 Thread.sleep 包围在 try/catch 中,并确保线程在您希望的时间内休眠。它看起来像这样:
long wakeTime = new Date().getTime() + (1000 * 15);
while ((new Date()).getTime() < wakeTime) {
try {
Thread.sleep(1000*15);
} catch (InterruptedException e) {
// do nothing
}
}
我怀疑您的线程由于信号而提前唤醒,这可能是因为您在 threadExecutor.execute(a) 之后立即调用了 threadExecutor.shutdown()。您可能还想考虑调用 threadExecutor.awaitTermination()。
得知任务从未执行后编辑:
因为 threadExecutor.shutdown() 不等待任务完成,看起来您的程序正在立即退出。在调用 threadExecutor.shutdown() 之后,您应该尝试使用 threadExecutor.awaitTermination(),将其置于类似于为 Thread.sleep() 所建议的循环中。
一个建议是让"threadExecutor"成为一个静态成员变量
服务器。
如果你只想要 n 个客户端,那么让池有 n 个线程
ExecutorService threadExecutor = Executors.newFixedThreadPool(n);
在执行方法 id 中关闭似乎不正确。
只有当您决定关闭该池时,才应关闭该池
服务器。
到那时它应该处于活动状态并处理客户端请求。
所以你必须删除 shutdown 和 newFixedThreadPool 语句
在 executeJob 方法之外。
摆脱线程池并使用计数信号量来控制上传的内联执行。
我在客户端调用的函数上有一个线程池..只让 (n) 个客户端执行此函数 upload()
,其他客户端等待..我试图调用 sleep()
该功能的实现,但它没有工作...
注意:我这样做是为了有时间查看其他客户端不执行该功能,而有 (n) 个客户端执行它...
我需要快速帮助..
服务器代码:
public class Server extends UnicastRemoteObject implements ExcutorInterface
{
public Server()throws RemoteException
{
System.out.println("Server is in listening mode");
}
public static void main(String arg[]) throws InterruptedException
{
try{
LocateRegistry.createRegistry(1234);
Server p=new Server();
Naming.bind("//127.0.0.1:1234/obj",p);
}catch(Exception e)
{
System.out.println("Exception occurred : "+e.getMessage());
}
}
@Override
public void executeJob() throws RemoteException {
System.out.println("Inside executeJob...");
doJob a=new doJob("req_id","usrname","pwd");
ExecutorService threadExecutor = Executors.newFixedThreadPool(2);
threadExecutor.execute(a);
threadExecutor.shutdown();
}
}
doJob代码:
public class doJob implements Runnable {
String request_id="", usrnamee="", pswd="";
public static int i = 1;
public doJob(String request_id,String usrnamee,String pswd) {
this.request_id=request_id;
this.usrnamee=usrnamee;
this.pswd=pswd;
}
public void upload() throws InterruptedException, IOException {
Thread.sleep(1000*15);
}
public void run() {
upload();
}
}
我在客户端调用 executeJob();
希望Thread.sleep()能帮您解决。 你也可以使用 wait().
为了详细说明我的评论,您应该将 Thread.sleep 包围在 try/catch 中,并确保线程在您希望的时间内休眠。它看起来像这样:
long wakeTime = new Date().getTime() + (1000 * 15);
while ((new Date()).getTime() < wakeTime) {
try {
Thread.sleep(1000*15);
} catch (InterruptedException e) {
// do nothing
}
}
我怀疑您的线程由于信号而提前唤醒,这可能是因为您在 threadExecutor.execute(a) 之后立即调用了 threadExecutor.shutdown()。您可能还想考虑调用 threadExecutor.awaitTermination()。
得知任务从未执行后编辑:
因为 threadExecutor.shutdown() 不等待任务完成,看起来您的程序正在立即退出。在调用 threadExecutor.shutdown() 之后,您应该尝试使用 threadExecutor.awaitTermination(),将其置于类似于为 Thread.sleep() 所建议的循环中。
一个建议是让"threadExecutor"成为一个静态成员变量 服务器。 如果你只想要 n 个客户端,那么让池有 n 个线程 ExecutorService threadExecutor = Executors.newFixedThreadPool(n);
在执行方法 id 中关闭似乎不正确。
只有当您决定关闭该池时,才应关闭该池 服务器。
到那时它应该处于活动状态并处理客户端请求。 所以你必须删除 shutdown 和 newFixedThreadPool 语句 在 executeJob 方法之外。
摆脱线程池并使用计数信号量来控制上传的内联执行。