简体   繁体   English

我们可以将线程连接方法放入同步方法中吗

[英]can we put thread join method inside synchronized method

Can I use thread.join inside a synchronized method? 我可以在同步方法中使用thread.join吗?

class Table{  
 synchronized void printTable(int n){//synchronized method  
   for(int i=1;i<=5;i++){  
     System.out.println(n*i);  
     try{  
      Thread.join(); 
     }catch(Exception e){System.out.println(e);}  
   }  
 }  
}  

can I use thread.join instead of wait? 我可以使用thread.join而不是等待吗?

First of all: it is not a static method so Thread.join wont work. 首先: 它不是静态方法,所以Thread.join将无法工作。
Yes you can call. 是的,您可以打电话。 No compilation or runtime exceptions but please consider below two points may be you don't want want to do it after that. 没有编译或运行时异常,但是请考虑以下两点,因为您可能不想在此之后这样做。 :) :)
Joining a thread means that one waits for the other to end, so that you can safely access its result or continue after both have finished their jobs.All overloaded versions of join() are final and synchronized. 加入一个线程意味着一个线程等待另一个线程结束,这样您就可以安全地访问它的结果,也可以在两个线程都完成其工作之后继续执行。join()的所有重载版本都是最终的且已同步。
Synchronized methods enable a simple strategy for preventing thread interference and memory consistency errors: if an object is visible to more than one thread, all reads or writes to that object's variables are done through synchronized methods. 同步方法提供了一种防止线程干扰和内存一致性错误的简单策略:如果一个对象对多个线程可见,则对该对象变量的所有读取或写入都将通过同步方法完成。

It's very unclear what you're trying to do here. 尚不清楚您要在这里做什么。 It appears you might be misunderstanding what the synchronized keyword does and where the locks are that are used in your example. 看来您可能误会了synced关键字的作用以及示例中使用的锁在哪里。

The line Thread.join(); Thread.join(); is not valid since it's an instance method. 无效,因为它是一个实例方法。 It's not apparent what thread you actually want to join. 您实际要加入哪个线程还不清楚。 You would need to provide a reference to whatever thread it is whose termination you want to wait for. 您需要提供对要等待其终止的线程的引用。

Here's the Java tutorial description of Thread#join : 是Thread#joinJava教程描述

The join method allows one thread to wait for the completion of another. join方法允许一个线程等待另一个线程的完成。 If t is a Thread object whose thread is currently executing, 如果t是当前正在执行线程的Thread对象,

t.join();

causes the current thread to pause execution until t's thread terminates. 导致当前线程暂停执行,直到t的线程终止。 Overloads of join allow the programmer to specify a waiting period. 连接的重载使程序员可以指定等待时间。 However, as with sleep, join is dependent on the OS for timing, so you should not assume that join will wait exactly as long as you specify. 但是,与睡眠一样,join的运行时间也取决于操作系统,因此,您不应假定join会完全按照您指定的时间等待。

It's not apparent why you would perform this join in a for-loop, because the join method waits until the thread is completely finished (as in no longer alive), multiple calls to it wouldn't seem useful. 尚不清楚为什么要在for循环中执行此联接,因为join方法要等到线程完全完成(因为不再处于活动状态)时,多次调用它似乎没有用。 (Are there multiple threads you need the current thread to join to? Or you're expecting to provide a timeout value and repeatedly try to join the same thread?) (是否有多个线程需要当前线程加入?或者您希望提供一个超时值并反复尝试加入同一线程?)

In your comment, when you ask: 在您的评论中,当您问:

simple question about can I use thread.join instead of wait 我可以使用thread.join代替wait的简单问题

you can't use one as a drop-in replacement for the other. 您不能使用其中一种替代另一种。 The lock held by the synchronized method is not the same as the lock used (acquired, released, then re-acquired) by the join. 同步方法持有的锁与联接所使用(获取,释放然后重新获取)的锁不同。 The join method uses the lock on the thread, wait has to use the lock on the Table instance (the lock used by the synchronized method). join方法使用线程上的锁,等待时必须使用Table实例上的锁(同步方法使用的锁)。 Calling wait from within the synchronized method will release the lock on the Table instance (giving other threads a chance to access that instance), but calling join will not. 从同步方法中调用wait将释放Table实例上的锁定(使其他线程有机会访问该实例),但调用join则不会。 Since the synchronized method is holding a lock on the instance of Table, that means your thread is denying any other thread access to that Table instance until whatever thread it's joining finishes. 由于同步方法在Table实例上拥有一个锁,这意味着您的线程将拒绝对该Table实例的任何其他线程访问,直到它加入的线程完成为止。 Although join is implemented using wait, what it's waiting for is a notification from the monitor on the thread you're joining, not on the Table object, so the lock on the Table instance never gets released until the method completes, which depends on the joins completing. 尽管连接是使用wait实现的,但是它所等待的是来自正​​在监视的线程上的监视器的通知,而不是Table对象的通知,因此,在该方法完成之前,不会释放对Table实例的锁定,具体取决于方法。加入完成。 The thread is going dormant while holding a lock; 按住锁时线程正在休眠; if other threads need to access this Table object then you are denying those other threads access with this approach. 如果其他线程需要访问此Table对象,则您将拒绝使用此方法访问其他线程。

So (assuming you add some logic to provide the reference to whatever thread or threads that your current thread needs to join to) you can do this, but it seems horrible. 因此(假设您添加了一些逻辑以提供对当前线程需要加入的一个或多个线程的引用),您可以执行此操作,但这似乎很可怕。 Threads should minimize the time they spend holding locks. 线程应尽量减少花费在持有锁上的时间。 Possibly a higher-level construct like CyclicBarrier could be useful here (though you still shouldn't hold onto a lock while doing this either). 可能像CyclicBarrier之类的更高级别的构造在这里可能很有用(尽管您也不应该在执行此操作的同时锁定它)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我们可以在Java中调用对象构造函数内的对象的synchronized方法吗? - Can we call synchronized method of an object inside the constructor of the object in Java? 如何检查线程是否在同步块或方法中? - How can I check if a thread is inside a synchronized block or method? 类具有2个同步方法。.当一个线程位于第二个方法内时,另一个线程可以进入第一个方法吗? - Class has 2 synchronized methods..when a thread is inside second method can another thread enter into first method? 在同步方法内的线程中同步块会发生什么? - What would happen with a synchronized block in a thread which is inside a synchronized method? 同步方法中的线程锁定 - Thread locking in synchronized method 线程子类中的同步方法 - Synchronized method in thread subclass 在另一个同步方法中引用同步方法 - Referencing a synchronized method inside another synchronized method ActionForm内部的同步方法 - Synchronized method inside ActionForm 从Java中同一类的另一个同步方法内部创建的新线程中调用同步方法 - Calling a synchronized method from a new thread created inside another synchronized method of the same class in Java 等待线程是否重新访问synchronized方法中的代码 - Does the waiting thread revisit the code inside synchronized method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM