Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12141

yield() inherited, yet it can not be invoked without prefix Thread, why? [closed]

$
0
0

In reference to the code below, the class MyThread inherits from the Thread class its methods yield() and sleep(). The below code does not compile, the IDE (Eclipse 2022-2023 using JDK SE17) hints to use Thread.yield(), while accepting sleep(). Both methods are inherited by MyThread and hence can be invoked directly. I have the same problem while using the online compiler jdoodle. The latter gives the below error. Any explanation, please?

Compilation error:

invalid use of a restricted identifier 'yield'           yield();                     ^  (to invoke a method called yield, qualify the yield with a receiver or type name)
class MyThread extends Thread {    public void run() {        System.out.println("Hi");        yield();                  try {            sleep(500);        }        catch(Exception v) {        }    }}public class JavaYield {    public static void main(String args[]) {         new MyThread().start();    }}

Viewing all articles
Browse latest Browse all 12141

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>