Giter Club home page Giter Club logo

Comments (8)

FongMi avatar FongMi commented on May 31, 2024 1

感謝提供範例,我試試看。

from quickjs-wrapper.

FongMi avatar FongMi commented on May 31, 2024 1

這個 Timer 可行,太棒了,謝謝。

from quickjs-wrapper.

FongMi avatar FongMi commented on May 31, 2024

哦 看來不適合我的專案
我的 ctx 是在一個 newSingleThreadExecutor 下建立的
無法使用 new Handler 再去跑 js func

from quickjs-wrapper.

HarlonWang avatar HarlonWang commented on May 31, 2024

newSingleThreadExecutor 里的线程你改成 HandlerThread 的话,就可以使用 Handler.post 来实现 setTimeout 了

from quickjs-wrapper.

HarlonWang avatar HarlonWang commented on May 31, 2024

可以参考下:
image

from quickjs-wrapper.

FongMi avatar FongMi commented on May 31, 2024

測試好像不行

可能是因為前面 thread.start(); 啟動過了
下面這個如果有 submit runnable 進來
Executors.newSingleThreadExecutor(r -> thread);
thread 會在 strart() 一次

會出現 java.lang.IllegalThreadStateException

from quickjs-wrapper.

HarlonWang avatar HarlonWang commented on May 31, 2024

估计 Executors 对 Thread 有一些特殊处理,那你把前面的 thread.start 去掉试试呢?如果不行的话,建议下面两个方案:

  1. 直接使用 HandlerThread 替代 Executors.newSingleThreadExecutor
  2. 使用 TimerTask 来实现延迟执行,或者其他可以实现延迟功能的 api
    类似代码如下:
    private final Timer timer = new Timer("timer");

    public Spider() {
    }

    public Spider(String api) {
        this.executor = Executors.newSingleThreadExecutor();
        this.key = "__" + UUID.randomUUID().toString().replace("-", "") + "__";
        this.api = api;
    }

    public void setTimeout() {
        this.executor.submit(() -> {
            // In Javascript test: setTimeout(1000, () => { console.log('executed'); })
            ctx.getGlobalObject().setProperty("setTimeout", args -> {
                int delayTime = (int) args[0];
                JSFunction callback = (JSFunction) args[1];
                callback.hold();
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        // 这里需要切回到原线程执行
                        executor.submit(() -> {
                            callback.call();        
                        });
                    }
                }, delayTime);
                return null;
            });
        });
    }

from quickjs-wrapper.

FongMi avatar FongMi commented on May 31, 2024

好的,感謝,我在調整看看。

from quickjs-wrapper.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.