ECTE331 Tutorial – Real Time Java Specification- Tutorial Solution Question 1 What will be the output of the following program when run on a RTOS with a single core processor? public static class MyRT...

1 answer below »
I have attached 2 files. the image file is the question itself, and you can use the tutorial8 file to help answer the question. PLEASE HAVE EXPERT ANKUSH WORK ON THIS.


ECTE331 Tutorial – Real Time Java Specification- Tutorial Solution Question 1 What will be the output of the following program when run on a RTOS with a single core processor? public static class MyRT extends RealtimeThread { int threadNum; public MyRT(int num, int priority) { this.threadNum = num; this.setPriority(priority); } public void run() { System.out.println("Thread#" + threadNum + " Starting"); while(!this.isInterrupted()) { System.out.println("Thread#" + threadNum + " Working"); //Perform an operation which take 200ms some200msOperation(); } System.out.println("Thread#" + threadNum + " Ending"); } } public static void main(String[] args) throws InterruptedException { Thread.currentThread().setPriority(20); MyRT rt1 = new MyRT(1, 11); rt1.start(); Thread.sleep(1000); MyRT rt2 = new MyRT(2, 12); rt2.start(); Thread.sleep(1000); MyRT rt3 = new MyRT(3, 13); rt3.start(); Thread.sleep(1000); rt1.interrupt(); rt2.interrupt(); rt3.interrupt(); } Answer: Thread#1 Starting Thread#1 Working Thread#1 Working Thread#1 Working Thread#1 Working Thread#1 Working The main thread priority is set to 20; it runs until Thread.sleep(1000) instruction where it’ll be put in waiting state for 1000 ms. Meanwhile, the start() method of thread rt1 would have already been invoked and hence rt1 is on a Runnable state, waiting to execute as soon as the main() thread stops; this happens when Thread.sleep(1000) is executed. Thread rt1 runs then for 1000 ms, which allows for 5 iterations. Therefore, the first line before the for is printed, then the print instruction is executed 5 times. Then the main() is waken up and the CPU scheduler, being a fully-preemptive priority based, selects it over rt1 as it has a higher priority (20) Thread#2 Starting Thread#2 Working Thread#2 Working Thread#2 Working Thread#2 Working Thread#2 Working The main() thread launches rt2 which becomes runnable, i.e. eligible for execution but not yet running after setting its priority to 12, lower than 20 but higher than rt1’s priority. The main() thread sleeps then for 1000 ms, allowing rt2 to step in to run for 5 iterations. After 1000 ms, the main() is waken up and takes over again as it has the highest priority in the system Thread#3 Starting Thread#3 Working Thread#3 Working Thread#3 Working Thread#3 Working Thread#3 Working The same analysis as above Thread#3 Ending Thread#2 Ending Thread#1 Ending The main() takes over as it has the highest priority in the system. It sends interrupt to the three threads , then exits. The remaining threads finish their execution in order of their respective priority values. Question 2 Implement and verify Java program (using RTSJ) that uses a Real Time Thread to print the string “Hello RT world” periodically every 500 milliseconds for 20 times. Hint, create a subclass extending the RealtimeThread class (other implementations are possible). Answer: import javax.realtime.*; public class PeriodicRTHello extends RealtimeThread { PeriodicRTHello(PeriodicParameters pp){ // In the following, I am calling the constructor of RealtimeThread using the reserved word //super with PriorityParameters set to null as it was not specified in the question // see slide 10 of SchedulableObjectclasses notes. super(null, pp); } public void run(){ for(int i = 0; i < 20;="" i++)="" {="" system.out.println("hello="" rt="" world!="" "="" +="" i);="" waitfornextperiod();="" }="" }="" public="" static="" void="" main(string="" []="" args){="" since="" the="" start="" time="" is="" not="" specified,="" i="" am="" using="" the="" third="" constructor="" of="" periodicparameters="" to="" set="" the="" period="" to="" 500="" ms="" and="" 0="" ns;="" see="" the="" link="" below="" https://docs.oracle.com/javase/realtime/doc_2.2u1/release/rtsj-docs/javax/realtime/periodicparameters.html="" periodicparameters="" pp="new" periodicparameters(new="" relativetime(500,="" 0));="" periodicrthello="" rtt="new" periodicrthello(pp);="" rtt.start();="" try="" {="" rtt.join();="" }="" catch="" (interruptedexception="" e)="" {="" e.printstacktrace();="" }="" system.out.println("main="" done!");="" }="" }="" */="" question="" 3="" implement="" and="" verify="" java="" program="" (using="" rtsj)="" that="" uses="" a="" real="" time="" thread="" to="" print="" the="" string="" “hello="" rt="" world”="" periodically="" every="" 500="" milliseconds="" for="" 20="" times.="" use="" a="" relative="" deadline="" of="" 100="" milliseconds="" and="" attach="" an="" event="" handler="" for="" deadline="" miss="" events.="" the="" event="" handler="" prints="" a="" message="" and="" reschedule="" the="" real="" time="" thread.="" verify="" the="" functionality="" by="" intentionally="" missing="" some="" deadline="" (i.e.="" sleep).="" answer:="" import="" javax.realtime.*;="" public="" class="" misshdlr="" extends="" asynceventhandler="" {="" realtimethread="" th;="" public="" void="" setthread(realtimethread="" pth)="" {="" th="pth;" }="" misshdlr()="" {="" we="" assume="" to="" launch="" the="" handler="" with="" max_priority="" level="" super(new="" priorityparameters(priorityscheduler.max_priority),="" null,="" null,="" null,="" null,="" null);="" }="" public="" void="" handleasyncevent()="" {="" system.out.println("recovering="" from="" a="" deadline="" miss");="" th.scheduleperiodic();="" }="" }="" public="" class="" periodicrthello="" extends="" realtimethread="" {="" periodicrthello(periodicparameters="" pp){="" super(null,="" pp);="" }="" public="" void="" run(){="" for(int="" i="0;" i="">< 20; i++) { system.out.println("hello rt world! " + i); if((i%2)==0) { try { realtimethread.sleep(new relative(200,0)); // introduced on purpose to miss deadlines } catch (interruptedexception e) { e.printstacktrace(); } } waitfornextperiod(); } } public static void main(string [] args){ //see slides 17-20 of 3.schedulabeobjectclasses for a similar question misshdlr misshdlr = new misshdlr(); // don’t start misshdlr yet…. periodicparameters p = new periodicparameters( new relativetime(0,0), // start at .start() new relativetime(500, 0), // 500ms period null, // no cost new relativetime(100,0), // 100 ms deadline null, // no overrun handler misshdlr); // miss handler periodicrthello rtt = new periodicrthello(p); misshdlr.setthread(rtt); rtt.start(); try { rtt.join(); } catch (interruptedexception e) { e.printstacktrace(); } system.out.println("main done!"); } } page 4 of 4 20;="" i++)="" {="" system.out.println("hello="" rt="" world!="" "="" +="" i);="" if((i%2)="=0)" {="" try="" {="" realtimethread.sleep(new="" relative(200,0));="" introduced="" on="" purpose="" to="" miss="" deadlines="" }="" catch="" (interruptedexception="" e)="" {="" e.printstacktrace();="" }="" }="" waitfornextperiod();="" }="" }="" public="" static="" void="" main(string="" []="" args){="" see="" slides="" 17-20="" of="" 3.schedulabeobjectclasses="" for="" a="" similar="" question="" misshdlr="" misshdlr="new" misshdlr();="" don’t="" start="" misshdlr="" yet….="" periodicparameters="" p="new" periodicparameters(="" new="" relativetime(0,0),="" start="" at="" .start()="" new="" relativetime(500,="" 0),="" 500ms="" period="" null,="" no="" cost="" new="" relativetime(100,0),="" 100="" ms="" deadline="" null,="" no="" overrun="" handler="" misshdlr);="" miss="" handler="" periodicrthello="" rtt="new" periodicrthello(p);="" misshdlr.setthread(rtt);="" rtt.start();="" try="" {="" rtt.join();="" }="" catch="" (interruptedexception="" e)="" {="" e.printstacktrace();="" }="" system.out.println("main="" done!");="" }="" }="" page="" 4="" of="">
Answered 14 days AfterJun 23, 2021

Answer To: ECTE331 Tutorial – Real Time Java Specification- Tutorial Solution Question 1 What will be the...

Parth answered on Jul 08 2021
130 Votes
import java.util.Random;
public class PeriodicRThread extends Thread {
    
    final Integer tempeartu
re = 15;
    
    @Override
    public void run() {
        try {
            for(int i=0; i<1; i++) {        
                Random random = new Random();
                Integer num = random.nextInt(20 - 10 + 1) + 10;
                System.out.println("Number : " + num);        
                if(num == tempearture)...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here