PLEASE USING WITH ONLY JAVAFX library , THANK YOU! My coding: import java.util.*
ID: 3751287 • Letter: P
Question
PLEASE USING WITH ONLY JAVAFX library, THANK YOU!
My coding:
import java.util.*; // for date
public class Lab04Inner{
public static void main (String [] args) throws InterruptedException{
firstThread ft1 = new firstThread(); // Being with the first thread
secondThread st1 = new secondThread(); // Being with the second thread
ft1.start(); // First thread starts
st1.start(); // Second thread starts
synchronized(ft1){
ft1.wait();
System.out.println("FT1: The object has been notified by the thread!");
}
synchronized(st1){
ft1.wait();
System.out.println("MT: The object has been notified by the thread!");
}
}
}
class firstThread extends Thread {
public void run() {
for(int count = 1; count <= 1; count++)
System.out.println("This ran thread with first thread.");
System.out.println(" Program finished with first thread");
yield();
}
}
class secondThread extends Thread {
public void run() {
for(int counter = 0; counter <= 1; counter++)
System.out.println("This ran thread with second thread.");
System.out.println(" Program finished with second thread");
yield();
}
}
Explanation / Answer
1.a
Program: Lab04.java
//Declaring the class
public class Lab04{
//declaring the constructor
public Lab04() {
Lab04Inner lb1=new Lab04Inner("Thread 1");//creating object of the inner class and passing the name of the thread
Lab04Inner lb2=new Lab04Inner("Thread 2");//creating object of the inner class and passing the name of the thread
lb1.start();//starting the first thread
lb2.start();//starting the second thread
System.out.println("Program Finished ");//printing the message to the user
//end of the constructor
}
//main function to execute the program
public static void main(String[] args) {
Lab04 ob=new Lab04();//creating object of the Lab04 class to execute the threads
//end of the main function
}
//declaring the inner class
class Lab04Inner extends Thread{//extending thread class
String name;//variable to store thread nasme
public Lab04Inner(String nm){
super(nm);//calling the base class constructor to initialize a thread
name=nm;//storing the name of the thread to the local variable.
}
public void run(){
System.out.println("The ran thread is = "+Thread.currentThread().getName());//printing the name of the current thread that called the run function
}
//end of the ineer class
}
//end of the class
}
Output:
Program Finished
The ran thread is = Thread 2
The ran thread is = Thread 1
1.b
Program: Lab04.java
//Declaring the class
public class Lab04{
//declaring the constructor
public Lab04() {
Lab04Inner lb1=new Lab04Inner("Thread 1");//creating object of the inner class and passing the name of the thread
Lab04Inner lb2=new Lab04Inner("Thread 2");//creating object of the inner class and passing the name of the thread
lb1.start();//starting the first thread
lb2.start();//starting the second thread
System.out.println("Program Finished ");//printing the message to the user
//end of the constructor
}
//main function to execute the program
public static void main(String[] args) {
Lab04 ob=new Lab04();//creating object of the Lab04 class to execute the threads
//end of the main function
}
//declaring the inner class
class Lab04Inner extends Thread{//extending thread class
String name;//variable to store thread nasme
public Lab04Inner(String nm){
super(nm);//calling the base class constructor to initialize a thread
name=nm;//storing the name of the thread to the local variable.
}
public void run(){
yield();//if any other process need to be run they can, else the current process will continue to run
System.out.println("The ran thread is = "+Thread.currentThread().getName());//printing the name of the current thread that called the run function
}
//end of the ineer class
}
//end of the class
}
Output:
Program Finished
The ran thread is = Thread 1
The ran thread is = Thread 2
No change in the output as the processor is fast enough to execute the program
1.c
Program: Lab04.java
//Declaring the class
public class Lab04{
//declaring the constructor
int counter=0;//declaring and intiallizing the int attribute used as counter
public Lab04() {
Lab04Inner lb1=new Lab04Inner("Thread 1");//creating object of the inner class and passing the name of the thread
Lab04Inner lb2=new Lab04Inner("Thread 2");//creating object of the inner class and passing the name of the thread
lb1.start();//starting the first thread
lb2.start();//starting the second thread
System.out.println("Program Finished count = "+counter);//printing the message to the user
//end of the constructor
}
//main function to execute the program
public static void main(String[] args) {
Lab04 ob=new Lab04();//creating object of the Lab04 class to execute the threads
//end of the main function
}
//declaring the inner class
class Lab04Inner extends Thread{//extending thread class
String name;//variable to store thread nasme
public Lab04Inner(String nm){
super(nm);//calling the base class constructor to initialize a thread
name=nm;//storing the name of the thread to the local variable.
}
public void run(){
yield();//if any other process need to be run they can, else the current process will continue to run
System.out.println("The ran thread is = "+Thread.currentThread().getName());//printing the name of the current thread that called the run function
counter++;
}
//end of the ineer class
}
//end of the class
}
Output:
run:
Program Finished count = 0
The ran thread is = Thread 2
The ran thread is = Thread 1
BUILD SUCCESSFUL (total time: 0 seconds)
Value of the counter is 0 because the main thread got executed first before the other two thread could make changes to the counter value. There is no synchronization between the threads.
1.d
Program: Lab04.java
//Declaring the class
public class Lab04{
//declaring the constructor
int counter=0;//declaring and intiallizing the int attribute used as counter
public Lab04() throws InterruptedException {
Lab04Inner lb1=new Lab04Inner("Thread 1");//creating object of the inner class and passing the name of the thread
Lab04Inner lb2=new Lab04Inner("Thread 2");//creating object of the inner class and passing the name of the thread
lb1.start();//starting the first thread
lb2.start();//starting the second thread
lb1.join();//waiting all the threads to complete before executing the next statements
System.out.println("Program Finished count = "+counter);//printing the message to the user
//end of the constructor
}
//main function to execute the program
public static void main(String[] args) throws InterruptedException {
Lab04 ob=new Lab04();//creating object of the Lab04 class to execute the threads
//end of the main function
}
//declaring the inner class
class Lab04Inner extends Thread{//extending thread class
String name;//variable to store thread nasme
public Lab04Inner(String nm){
super(nm);//calling the base class constructor to initialize a thread
name=nm;//storing the name of the thread to the local variable.
}
public void run(){
yield();//if any other process need to be run they can, else the current process will continue to run
System.out.println("The ran thread is = "+Thread.currentThread().getName());//printing the name of the current thread that called the run function
counter++;
}
//end of the ineer class
}
//end of the class
}
Output:
run:
The ran thread is = Thread 1
The ran thread is = Thread 2
Program Finished count = 2
BUILD SUCCESSFUL (total time: 0 seconds)
I have used join function which will make all the thread to wait for the other thread to complete their execution before further processing the subsequent statement.
So all the three thread i.e. main,thread 1 and thread 2 completed their execution and then the message to the user was displayed so the counter value was incremented 2 times and printed as 2.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.