[go: up one dir, main page]

0% found this document useful (0 votes)
4 views2 pages

WEEK 6 Code

The document contains code snippets for various questions related to Java threading. It includes extending the Thread class, creating a main method, defining a MyThread class, and implementing synchronized methods for summing and incrementing variables. Each question builds upon concepts of threading and synchronization in Java programming.

Uploaded by

ra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

WEEK 6 Code

The document contains code snippets for various questions related to Java threading. It includes extending the Thread class, creating a main method, defining a MyThread class, and implementing synchronized methods for summing and incrementing variables. Each question builds upon concepts of threading and synchronization in Java programming.

Uploaded by

ra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

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");

You might also like