java线程等待和自动唤醒

java线程等待和自动唤醒,java,multithreading,Java,Multithreading,我有一个web应用程序,它接受来自用户的一些数据来创建一个任务,然后应该执行该任务 由于任务的执行是从互联网下载一些东西,这将花费一些时间,所以我尝试创建一个新线程来完成这项工作 这是我的想法: 创建用于下载数据的LoaderThread。而LoaderThread保存一个ArrayList字段,用于放置任务 处理请求和响应的Servlet 启动Servlet时,启动LoaderThread 在servlet运行期间,将任务添加到LoaderThread 这是代码(其中一些代码被省略): 如果t

我有一个web应用程序,它接受来自用户的一些数据来创建一个任务,然后应该执行该任务

由于任务的执行是从互联网下载一些东西,这将花费一些时间,所以我尝试创建一个新线程来完成这项工作

这是我的想法:

  • 创建用于下载数据的
    LoaderThread
    。而
    LoaderThread
    保存一个
    ArrayList
    字段,用于放置
    任务

  • 处理请求和响应的
    Servlet

  • 启动
    Servlet
    时,启动
    LoaderThread

  • 在servlet运行期间,将任务添加到
    LoaderThread

  • 这是代码(其中一些代码被省略):

    如果
    tasks
    为空,我尝试调用
    wait()

    但我不知道如何唤醒它

    还有,我应该知道什么来改进应用程序吗

    BWT,是否有可能创建多个
    LoaderThread
    实例?如果是,如何避免


    看起来我可以使用其他实现,但我想知道我的案例是否可以重构


    因为我想学一些我错过的东西。:)谢谢

    您的要求是的标准用法,因此我建议您使用,而不是重新发明轮子

    根据您提供的代码,您的servlet应该如下所示:

    public class RwdServlet extends HttpServlet {
        private ExecutorService loader;
    
        @Override
        public void init() throws ServletException {
            super.init();
            loader = Executors.newCachedThreadPool();//or use some other executor, google about difference between them
        }
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            Task t=createTask(req); //assume that task implements Runnable or Callable
            loader.submit(t); // submit a task to executor after this line your task will start execution in another thread
        }
        @Override
        public void destroy() {
            loader.shutdown();//this will destroy executor service but before that it will wait until all already submitted tasks will be executed
    
        }
    }
    

    请参阅您的用例调用ExecutorService,并且您已经开始从头开始重新实现它。最好现在停止使用标准库中已完成、无缺陷、灵活且功能强大的产品。

    对不起,我不太清楚您的意思。你建议我使用执行器服务吗?@hguser:我很确定他的意思是你正在尝试构建一些已经存在的东西,所以你应该停止浪费时间,使用更好的库。除非你拥有惊人的技能,否则你的成绩不会比现有成绩更好。:)@lzmaki:谢谢,我知道了。
    ExecutorService
    可以等待并自动唤醒吗?你说的自动唤醒是什么意思?在“待机模式”下初始化时和提交任务之前
    ExecutorService
    。提交任务后,它会开始异步计算。所有任务完成后,
    ExecutorService
    是否会再次处于“待机”状态,一段时间后,如果任务中添加了新任务,它是否会启动?是的,它的工作方式与您描述的相同。基本上,它会接受和处理任务,直到你对它执行
    shutdown()
    。嗨,我已经在谷歌上搜索了Executor服务,但我不知道如何在我的情况下使用它。例如,我可以在哪里保存
    任务列表
    ?你能抽出点时间给我举个例子吗?
    @Override
    public void run() {
        while (running) {
            if (tasks.size() > 0) {
                Task t = tasks.get(0);
                log.info(t);
                if (t != null && t.status == Status.waiting) {
                    tasks.remove(0);
                    t.status = Status.running;
                    downLoad(t);
                }
            } else {
                try {
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
    
            }
        }
    }
    
    public class RwdServlet extends HttpServlet {
        private ExecutorService loader;
    
        @Override
        public void init() throws ServletException {
            super.init();
            loader = Executors.newCachedThreadPool();//or use some other executor, google about difference between them
        }
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            Task t=createTask(req); //assume that task implements Runnable or Callable
            loader.submit(t); // submit a task to executor after this line your task will start execution in another thread
        }
        @Override
        public void destroy() {
            loader.shutdown();//this will destroy executor service but before that it will wait until all already submitted tasks will be executed
    
        }
    }