Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

thread.stop problem #14

Closed
levBagryansky opened this issue Sep 1, 2022 · 9 comments
Closed

thread.stop problem #14

levBagryansky opened this issue Sep 1, 2022 · 9 comments

Comments

@levBagryansky
Copy link
Member

levBagryansky commented Sep 1, 2022

It seems I cannot yet implement thread.stop.
This is implementation of run in DataizingThread.java:

    public Phi dataized() {
        return this.computed;
    }

    @Override
    public void run() {
        this.computed = new Data.ToPhi(new Dataized(this.eothread.attr("slow").get()).take());
    }

And stop in its dataization will do

      final Phi parent = rho.attr("ρ").get();
      final DataizingThread thr = Threads.INSTANCE.get(parent);
      thr.interrupt();
      return new Data.ToPhi(true);

So in DataizingThread.java it must be

    @Override
    public void run() {
        try {
            this.computed = new Data.ToPhi(new Dataized(this.eothread.attr("slow").get()).take());
        } catch (InterruptedException ie){
        }
    }

But compiler does not allow do that because
Exception 'java.lang.InterruptedException' is never thrown in the corresponding try block
So Dataized.get does not throw it. But Thread.sleep that is used in EOsleep.java can do.
@yegor256 @mximp may be we have other ways to stop the thread? I see only bad variants like deprecated Thread.stop and may be JNI. Or we need to mark some nethods in eo throws InterruptedException?

@mximp
Copy link

mximp commented Sep 1, 2022

@levBagryansky It's not possible in Java to stop a thread if execution code is not written for that.
In this case once you called new Data.ToPhi(new Dataized(this.eothread.attr("slow").get()).take()); there is no way to stop/interrupt it within JVM.
The only way to overcome this is to amend Dataized().get() to be interruptable.
@yegor256 what do you think?

@levBagryansky
Copy link
Member Author

@yegor256 please have a look

@yegor256
Copy link
Member

yegor256 commented Sep 7, 2022

@levBagryansky in Java there is not way to stop a thread unless the thread politely reacts to our signal. Read this, btw: https://www.yegor256.com/2015/10/20/interrupted-exception.html

So, I think, "do our best" and send the interrupted signal. If the thread stops -- great. If not, we can't do anything.

@levBagryansky
Copy link
Member Author

@yegor256 The problem is how we can handle interruption in thread

    @Override
    public void run() {
        try {
            this.computed = new Data.ToPhi(new Dataized(this.eothread.attr("slow").get()).take());
        } catch (InterruptedException ie){
        }
    }

But compiler does not allow do that because Exception 'java.lang.InterruptedException' is never thrown in the corresponding try block So Dataized.get does not throw it. But Thread.sleep that is used in EOsleep.java can do.

@yegor256
Copy link
Member

yegor256 commented Sep 7, 2022

@levBagryansky did you read this: https://www.yegor256.com/2015/10/20/interrupted-exception.html

This part:

while (/* You still need to wait */) {
    if (Thread.interrupted()) {
      throw new InterruptedException();
    }
    // Keep waiting
  }

@levBagryansky
Copy link
Member Author

@yegor256 We need to handle InterruptedExeption in get method of Atcompsite.java

@0pdd
Copy link

0pdd commented Sep 8, 2022

@levBagryansky the puzzle #24 is still not solved.

@0pdd
Copy link

0pdd commented Sep 25, 2022

@levBagryansky 2 puzzles #24, #45 are still not solved.

@0pdd
Copy link

0pdd commented Sep 26, 2022

@levBagryansky all 2 puzzles are solved here: #24, #45.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants