02/07/2020 Programming in Java - Course
https://onlinecourses.nptel.ac.in/noc20_cs08/progassignment?name=145 1/3
Due on 2020-03-12, 23:59 IST
Java Week 6: Q4
Execution of two or more threads occurs in a random order. The keyword 'synchronized' in Java is used to control the execution of thread in a strict sequence. In the following, the program is expected to print some numbers. Do the necessary use of 'synchronized' keyword, so that, the program prints the output in the following order:
---OUTPUT---
5 10 15 20 25 100 200 300 400 500
--- Sample Test Cases
Input Output
X
NPTEL (https://swayam.gov.in/explorer?ncCode=NPTEL) » Programming in Java (course)
(https://swayam.gov.in)
(https://swayam.gov.in/nc_details/NPTEL)Announcements (announcements)
About the Course (https://swayam.gov.in/nd1_noc20_cs08/preview) Ask a Question (forum) Progress (student/home) Mentor (student/mentor)
Course outline
How does an NPTEL online course work?
Week 0 : Week 1 : Week 2 : Week 3 : Week 4 : Week 5 : Week 6 :
Lecture 26 : Demonstration- X (unit?
unit=7&lesson=40) Lecture 27 : Multithreading- I (unit?
unit=7&lesson=41) Lecture 28 : Multithreading-
02/07/2020 Programming in Java - Course
https://onlinecourses.nptel.ac.in/noc20_cs08/progassignment?name=145 2/3
Test Case 1
5 10 15 20 25 100 200 300 400 500
Test Case 2
5 10 15 20 25 100 200 300 400 500
The due date for submitting this assignment has passed.
As per our records you have not submitted this assignment.
Sample solutions (Provided by instructor)
Week 7 : Week 8 : Week 9 : Week 10 : Week 11 : Week 12 : DOWNLOAD VIDEOS
II (unit?
unit=7&lesson=42) Lecture 29 : Demonstration- XI (unit?
unit=7&lesson=43) Lecture 30 : I- O Stream-I (unit?
unit=7&lesson=44) Quiz :
Assignment 6 (assessment?
name=98) Java Week 6:
Q1
(/noc20_cs08/progassignment?
name=142) Java Week 6:
Q2
(/noc20_cs08/progassignment?
name=143) Java Week 6:
Q3
(/noc20_cs08/progassignment?
name=144) Java Week 6:
Q4
(/noc20_cs08/progassignment?
name=145) Java Week 6:
Q5
(/noc20_cs08/progassignment?
name=146) Feedback For Week 6 (unit?
unit=7&lesson=154)
class Execute{
// Just add 'synchronized' in the method synchronized void print(int n){
for(int i=1;i<=5;i++){
System.out.println(n*i);
try{
Thread.sleep(400);
}catch(Exception e){
System.out.println(e);
} } }
} // Ending Execute class
class Thread1 extends Thread{
Execute t;
Thread1(Execute t){
this.t=t;
}
public void run(){
t.print(5);
} }
class Thread2 extends Thread{
Execute t;
Thread2(Execute t){
this.t=t;
}
public void run(){
t.print(100);
} }
public class Question64{
public static void main(String args[]){
Execute obj = new Execute();//only one object Thread1 t1=new Thread1(obj);
Thread2 t2=new Thread2(obj);
t1.start();
12 34 56 78 109 1112 1314 1516 1718 1920 2122 2324 2526 2728 2930 3132 3334 3536 3738 3940 41
02/07/2020 Programming in Java - Course
https://onlinecourses.nptel.ac.in/noc20_cs08/progassignment?name=145 3/3
Assignment Solution Books
Live Interactive Session
t2.start();
} } 4243 4445