The code would only execute when t1 was executing, in which case telling t2 to yield would be pointless - it's already yielded. So since the only thread worth calling yield on is the current thread, they make the method static so you won't waste time trying to call yield on some other thread. In the case of sleep: My guess is that sleep as an instance method would lead to deadlock problems similar to those of the now-deprecated suspend method.
Check out "Why are Thread. Problems with sleep wouldn't be as permanent, perhaps, as the sleep time limit would eventually expire, but you could still paralyze the JVM until that happens. I suppose you can do that anyway if you're careless, but I think the key is that if a thread is holding lock to critical resources, you should know not to call sleep until those locks are released.
If you do it anyway, it's your own fault. But if another thread were able to invoke sleep on this thread, it would have no way of knowing what section of code was being executed, and whether or not it's a good time for it. In general, it's better to have threads put themselves to sleep. If you want one thread to be able to tell another to go to sleep, you need to devise a means of communication between the threads, and have the target thread check periodically to see if it's been requested to go to sleep.
Improve this question. Harshal Patil 6, 8 8 gold badges 37 37 silver badges 56 56 bronze badges. Rajan Garg Rajan Garg 1 1 gold badge 3 3 silver badges 7 7 bronze badges. Add a comment. Active Oldest Votes. Improve this answer. Suresh Atta Suresh Atta k 37 37 gold badges silver badges bronze badges. Other threads are not necessarily yielded - if there are more than one processor core , then they can be active.
The true reason is that applying yield and sleep to other thread is unpredictable and dangerous, violating other threads' privacy. The question is about why that method is static?? Its not about accessing other threads. And a good a point here so far : — Suresh Atta.
Prasad Kharkar Prasad Kharkar I think wait is not a static method, reason is wait and notify methods used for communication between two threads while sleep and static is used inside of the current thread. No wait is not static, I meant to say that if you call wait method, then it causes current thread to stop until notify is called by other thread.
You are right regarding communication between two threads — Prasad Kharkar. But you can only call wait on the current thread if you have taken the current thread's lock. Mikhail Mikhail 4, 12 12 silver badges 30 30 bronze badges.
They are static so that overriding concept can be avoided i. FoldFence 2, 3 3 gold badges 28 28 silver badges 52 52 bronze badges. System having single processor has only one thread active at a time, so if thread A calls sleep on thread B object i. A responsible programmer would never define sleep inside a synchronized block, or when a thread is holding a lock.
But let's say thread A calls sleep method on thread B just when thread B is holding a lock or inside a synchronized block , thread B would go to sleep holding lock which is totally undesirable. Ankush G Ankush G 8 8 silver badges 13 13 bronze badges. Cloud Computing. Data Science. Angular 7. Machine Learning. Data Structures. Operating System. Computer Network. Compiler Design. Computer Organization.
Discrete Mathematics. Ethical Hacking. Computer Graphics. Software Engineering. Web Technology. Cyber Security. C Programming. Control System. Data Mining. Data Warehouse. Javatpoint Services JavaTpoint offers too many high quality services. The sleep Method Syntax: Following are the syntax of the sleep method.
0コメント