WEEK 6 Code:
Q No:1
// Write the appropriate code to extend the Thread class to complete the class
Question61.
public class Question61 extends Thread{
public void run(){
System.out.println("Thread is Running.");
}
-----------------------------------------------------------------------------------
Q No:2
// Create main() method and appropriate statements in it
public static void main(String[] args){
Question62 m1 = new Question62();
Thread m2 = new Thread(m1);
System.out.println("Welcome to Java Week 6 New Question.");
m2.setName("Main Thread");
m2.start();
//n2.start();
}
-----------------------------------------------------------------------------------
-
Q No:3
// Create a class named MyThread and extend/implement the required class/interface
// Define a method in MyThread class to print the output
// Class MyThread is defined which extends class B
class MyThread extends B {
// run() is overriden and 'NPTEL Java' is printed.
public void run() {
System.out.print("NPTEL Java week-6 new Assignment Q3");
}
}
-----------------------------------------------------------------------------------
-
Q No:4
// Returns the sum of a and b. (reader)
// Should always return an even number.
public synchronized int sum(){return(a+b);
}
// Increments both a and b. (writer)
public synchronized void inc() {
a++;
b++;
}
}
-----------------------------------------------------------------------------------
-
Q No:5
t1.setName("Week 6 Assignment Q5");
t2.setName("Week 6 Assignment Q5 New");