NPTEL
Code compiled and tested successfully!
Due date on 2026-09-17, 23:59 IST
Q.
Creating a Thread using Thread Class
Problem Statement
In Java, you can run multiple tasks at the same time using Multithreading.
The simplest way to create a thread is by extending the built-in Thread class.
What is a Thread?
A thread is a small unit of a program that runs independently
Multiple threads can run in parallel, improving efficiency
Programming Assignment:
Create a class called MyThread that extends Thread
In its run() method, print "Thread is running"
In the main method, create an object of MyThread and start the thread
This helps you understand the basic way to create and start a thread in Java.
Complete Program
public class W08_P1 { // Create a class that extends Thread static class MyThread extends Thread { @Override public void run() { // --- START OF SOLUTION CODE --- System.out.print("Thread is running"); // --- END OF SOLUTION CODE --- } } public static void main(String[] args) { // Create object of MyThread MyThread t = new MyThread(); // Start the thread t.start(); } }
Code Snippet to Paste in the Editor
// --- START OF SOLUTION CODE --- System.out.print("Thread is running"); // --- END OF SOLUTION CODE ---
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program, your assignment will not be graded and you will not see your score after the deadline.
Evaluation Results
Note: These tests may not be considered while scoring.
Due date on 2026-09-17, 23:59 IST
Q.
Problem Statement
In Java, another common way to create threads is by implementing the Runnable interface.
What is Runnable?
Runnable is an interface with a single method called run()
You can pass a Runnable object to a Thread and start the thread
Why use Runnable?
It allows your class to extend another class, as Java supports single inheritance
It provides flexibility in thread creation
Programming Assignment:
Create a class called MyRunnable that implements Runnable
In its run() method, print "Runnable thread is running"
In the main method, create a Thread object using MyRunnable and start the thread
This demonstrates thread creation using the Runnable interface.
Complete Program
public class W08_P2 { static class MyRunnable implements Runnable { @Override public void run() { // --- START OF SOLUTION CODE --- System.out.print("Runnable thread is running"); // --- END OF SOLUTION CODE --- } } public static void main(String[] args) { // Create object of MyRunnable MyRunnable r = new MyRunnable(); // Create Thread using Runnable object Thread t = new Thread(r); // Start the thread t.start(); } }
Code Snippet to Paste in the Editor
// --- START OF SOLUTION CODE --- System.out.print("Runnable thread is running"); // --- END OF SOLUTION CODE ---
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program, your assignment will not be graded and you will not see your score after the deadline.
Evaluation Results
Note: These tests may not be considered while scoring.
Due date on 2026-09-17, 23:59 IST
Q.
Programming Assignment: Understanding Basic Thread States in Java
Problem Statement
When a thread runs in Java, it moves through different stages called states.
What are Thread States?
Think of a thread like a person:
It starts in one state
Moves to another as work happens
Finally, it finishes
For beginners, focus on these three simple states:
New – The thread is created but not started yet
Running – The thread is doing its work
Terminated – The thread has finished its work
Programming Assignment:
Create a class called MyThread that extends Thread
Inside its run() method, print "Thread is running"
In the main method:
Create a MyThread object
Print "Thread state before start"
Start the thread
Print "Thread state after start"
Wait for thread to finish using join()
Print "Thread state after completion"
This shows how thread state changes as the thread runs.
Complete Program
public class W08_P3 { // Create a class that extends Thread static class MyThread extends Thread { @Override public void run() { // --- START OF SOLUTION CODE --- System.out.println("Thread is running"); // --- END OF SOLUTION CODE --- } } public static void main(String[] args) { // Create thread object MyThread t = new MyThread(); // Thread is created but not started yet System.out.println("Thread state before start"); // Start thread t.start(); // Thread has started running System.out.println("Thread state after start"); try { // Wait for thread to finish t.join(); } catch (InterruptedException e) { // Not needed for beginners, but required to handle possible interruptions } // Thread has finished System.out.println("Thread state after completion"); } }
Code Snippet to Paste in the Editor
// --- START OF SOLUTION CODE --- System.out.println("Thread is running"); // --- END OF SOLUTION CODE ---
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program, your assignment will not be graded and you will not see your score after the deadline.
Evaluation Results
Note: These tests may not be considered while scoring.
Due date on 2026-09-17, 23:59 IST
Q.
Understanding Thread Priority in Java
Problem Statement
In Java, each thread has a priority, a number from 1 (lowest) to 10 (highest).
Priority suggests how important a thread is, though actual scheduling depends on the system.
Programming Assignment:
Create a class MyThread that extends Thread
In the main method:
Create a MyThread object
Set its priority to 8
Start the thread
Print the thread's priority after setting
No output should come from the thread's run() method to avoid output mismatch.
Complete Program
public class W08_P4 { // Thread class static class MyThread extends Thread { @Override public void run() { // No output here to keep portal testing consistent } } public static void main(String[] args) { MyThread t = new MyThread(); // Set thread priority t.setPriority(8); // Start thread t.start(); // --- START OF SOLUTION CODE --- System.out.print("Thread priority is: " + t.getPriority()); // --- END OF SOLUTION CODE --- } }
Code Snippet to Paste in the Editor
// --- START OF SOLUTION CODE --- System.out.print("Thread priority is: " + t.getPriority()); // --- END OF SOLUTION CODE ---
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program, your assignment will not be graded and you will not see your score after the deadline.
Evaluation Results
Note: These tests may not be considered while scoring.
Due date on 2026-09-17, 23:59 IST
Q.
Programming Assignment: Introduction to Thread Synchronization
Problem Statement
What is a Thread?
Imagine your computer doing many tasks at once — for example:
Playing music
Downloading files
Browsing the internet
In Java, each small task that runs independently is called a Thread.
Threads help programs run faster by working at the same time.
Why Synchronization?
When multiple threads work on the same thing together, they may interfere with each other.
For example:
Two threads try to update the same number at the same time
The final result may be wrong
What is Synchronization?
It is like putting a lock
Only one thread can work on the shared thing at a time
This prevents problems caused by threads disturbing each other
Programming Assignment:
Create a class Counter with a number count starting from 0
Write a method increment() to increase the number by 1, using synchronized keyword
Create a thread class called MyThread that runs the increment() method 1000 times
In main, run two threads to increase the number
After both threads finish, print the final count
This shows how to use synchronization to avoid problems when multiple threads share data.
Complete Program
public class W08_P5 { // Shared class with a number static class Counter { int count = 0; // Synchronized method to safely increase number public synchronized void increment() { count++; } } // Thread class to run increment static class MyThread extends Thread { Counter c; MyThread(Counter c) { this.c = c; } @Override public void run() { // --- START OF SOLUTION CODE --- for (int i = 0; i < 1000; i++) { c.increment(); } // --- END OF SOLUTION CODE --- } } public static void main(String[] args) { Counter c = new Counter(); // Create two threads MyThread t1 = new MyThread(c); MyThread t2 = new MyThread(c); // Start both threads t1.start(); t2.start(); try { // Wait for both threads to finish t1.join(); t2.join(); } catch (InterruptedException e) { } // Print final count System.out.println("Final count is: " + c.count); } }
Code Snippet to Paste in the Editor
// --- START OF SOLUTION CODE --- for (int i = 0; i < 1000; i++) { c.increment(); } // --- END OF SOLUTION CODE ---
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program, your assignment will not be graded and you will not see your score after the deadline.
Evaluation Results
Note: These tests may not be considered while scoring.
No comments:
Post a Comment
Keep your comments reader friendly. Be civil and respectful. No self-promotion or spam. Stick to the topic. Questions welcome.