Java threads: wait and notify methods

后端 未结 7 1608
遥遥无期
遥遥无期 2020-12-04 00:28

I have a thread that calls the wait method and can only be awoken when the notify method called from some other class:

 class Threa         


        
7条回答
  •  我在风中等你
    2020-12-04 00:40

    do not synchronized(thread), don't do it, do not synchronized(thread).. repat: no synchronized(thread) :)

    And if you need to wait for the thread 'b' to finish, use b.join(), now your code is free to hang in b.wait()

    --

    Hopefully the source below can grant you an insight while sync(thread)/notify() I consider bad practice. (cut-cut)

    Enjoy


    To proceeed below you make sure you have accepted Oracle's License aggreement, found there: https://cds.sun.com/is-bin/INTERSHOP.enfinity/WFS/CDS-CDS_Developer-Site/en_US/-/USD/ViewLicense-Start?LicenseUUID=7HeJ_hCwhb4AAAEtmC8ADqmR&ProductUUID=pGqJ_hCwj_AAAAEtB8oADqmS&cnum=&evsref=&sln=

    Java sources (incl), called in init(), effectively called by any java c-tor, since java 1.5

    private static **synchronized int** nextThreadNum() {
    return threadInitNumber++;
    }
    

    //join (the method w/ nanos only increase millis by one, if nanos>500000, millis==0 and nanos>0

    public final **synchronized** void join(long millis) 
    throws InterruptedException {
    long base = System.currentTimeMillis();
    long now = 0;
    
    if (millis < 0) {
            throw new IllegalArgumentException("timeout value is negative");
    }
    
    if (millis == 0) {
        while (isAlive()) {
        wait(0);
        }
    } else {
        while (isAlive()) {
        long delay = millis - now;
        if (delay <= 0) {
            break;
        }
        wait(delay);
        now = System.currentTimeMillis() - base;
        }
    }
    }
    
    
    public **synchronized** void start() {
        /**
     * This method is not invoked for the main method thread or "system"
     * group threads created/set up by the VM. Any new functionality added 
     * to this method in the future may have to also be added to the VM.
     *
     * A zero status value corresponds to state "NEW".
         */
        if (threadStatus != 0)
            throw new IllegalThreadStateException();
        group.add(this);
        start0();
        if (stopBeforeStart) {
        stop0(throwableFromStop);
    }
    }
    

    //stop1 is called after stop ensures proper priviledges

    private final **synchronized** void stop1(Throwable th) {
    SecurityManager security = System.getSecurityManager();
    if (security != null) {
        checkAccess();
        if ((this != Thread.currentThread()) ||
        (!(th instanceof ThreadDeath))) {
        security.checkPermission(SecurityConstants.STOP_THREAD_PERMISSION);
        }
    }
        // A zero status value corresponds to "NEW"
    if (threadStatus != 0) {
        resume(); // Wake up thread if it was suspended; no-op otherwise
        stop0(th);
    } else {
    
            // Must do the null arg check that the VM would do with stop0
        if (th == null) {
        throw new NullPointerException();
        }
    
            // Remember this stop attempt for if/when start is used
        stopBeforeStart = true;
        throwableFromStop = th;
        }
    }
    

提交回复
热议问题