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(); }}