0% found this document useful (0 votes)
27 views10 pages

PPL Unit 5

The document covers various concepts in Java programming, specifically focusing on multithreading, including multitasking, multiprocessing, and different ways to implement threads. It explains methods like isAlive() and join(), the life cycle of threads, thread priority management, and thread synchronization. Additionally, it compares JavaScript frameworks React.js, Angular.js, and Vue.js, and lists features of JavaScript along with example programs.

Uploaded by

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

PPL Unit 5

The document covers various concepts in Java programming, specifically focusing on multithreading, including multitasking, multiprocessing, and different ways to implement threads. It explains methods like isAlive() and join(), the life cycle of threads, thread priority management, and thread synchronization. Additionally, it compares JavaScript frameworks React.js, Angular.js, and Vue.js, and lists features of JavaScript along with example programs.

Uploaded by

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

PPL UNIT 5

Q.1) Interpret the terms multitasking, multiprocessing, and


multithreading in Java with example. [6 Marks]
Answer:

1. Multitasking

Multitasking means executing multiple tasks (programs) at the same time.


This is managed by the operating system, where CPU switches between tasks
quickly.

📌 Example : Listening to music while writing a document.

Java Relation: Java applications can run on multitasking OS.

✅ 2. Multiprocessing
Multiprocessing is the use of two or more processors (CPUs) to perform tasks
in parallel.
It increases performance and speed.

Example: Running multiple JVMs or Java programs on a dual-core CPU


simultaneously.

✅ 3. Multithreading
Multithreading is a Java feature that allows multiple threads (small tasks) to
run concurrently within a single program.

It improves performance by using CPU efficiently.

✅ Multithreading Example:

class MyThread extends Thread {


public void run() {
System.out.println("Thread is running");
}
}

public class Test {


public static void main(String[] args) {
MyThread t1 = new MyThread();
t1.start(); // runs thread
}
}

By: Aaryan Waghmare and Chat GPT


PPL UNIT 5
Q.2) Explain different ways to implement Threads in Java with
code example. [6 Marks]

Answer: A thread is a smallest unit of execution..

Two Main Ways to Implement Threads in Java:

1. By Extending Thread Class

 Create a class that extends Thread.


 Override the run() method.
 Start the thread using start().

Example:

class MyThread extends Thread {


public void run() {
System.out.println("Thread using Thread class");
}
}

public class Test {


public static void main(String[] args) {
MyThread t = new MyThread();
t.start(); // starts the thread
}
}

2. By Implementing Runnable Interface

 Create a class that implements Runnable.


 Override the run() method.
 Pass the object to a Thread and call start().

Example:

class MyRunnable implements Runnable {


public void run() {
System.out.println("Thread using Runnable interface");
}
}

public class Test2 {


public static void main(String[] args) {
MyRunnable r = new MyRunnable();
Thread t = new Thread(r);
t.start();
}}

By: Aaryan Waghmare and Chat GPT


PPL UNIT 5
Q.3) Explain isAlive() and join() methods in
multithreading with example. [8 Marks]

Answer:
1. isAlive() Method

🔹 Definition:The isAlive() method is used to check whether a thread is still running


or not.
It returns:

 true if the thread is active (alive)


 false if the thread has completed execution

🔹 Syntax: threadObject.isAlive();

Example of isAlive() Method:

class MyThread extends Thread {


public void run() {
System.out.println("Thread is running");
}
}

public class Test {


public static void main(String[] args) {
MyThread t = new MyThread();
t.start();

System.out.println("Is thread alive? " + t.isAlive());


}
}
Output:
Child Thread: 1
Child Thread: 2
Child Thread: 3
Main Thread continues..

2. join() Method

 🔹 Definition:The join() method is used to pause the execution of the current


thread until the specified thread has finished executing. Useful when multiple threads
depend on each other

🔹 Syntax: threadObject.join();

By: Aaryan Waghmare and Chat GPT


PPL UNIT 5
✅ Example of join() Method:

class MyThread extends Thread {

public void run() {

System.out.println("Child thread running");

public class Test {

public static void main(String[] args) throws


InterruptedException {

MyThread t = new MyThread();

t.start();

t.join(); // Waits for t to finish

System.out.println("Main thread continues");

Output:

Child thread running


Main thread continues

Q.4) Explain life cycle of Thread model in Java. [6 Marks]


Answer:

By: Aaryan Waghmare and Chat GPT


PPL UNIT 5
🔹 1. New State

 The thread is created using the Thread class.


 Example: Thread t = new Thread();

🔹 2. Runnable State

 After calling t.start(), the thread enters the Runnable state.


 It is ready to run but waiting for CPU time.

 When CPU picks the thread, it starts executing.

🔹 3. Waiting / Timed Waiting / Blocked States

These are temporary states where the thread is paused:

 Waiting:
→ Thread waits indefinitely for another thread to notify.
→ Example: wait()
 Timed Waiting:
→ Thread waits for a specific time.
→ Example: sleep(1000), join(500)
 Blocked:
→ Thread is waiting for a input from the user to be given.

🔹 4. Terminated State (Dead)

 The thread has completed execution or exited due to an error.

Q.5) Elaborate the terms getPriority() and


setPriority() methods with example. [6 Marks]
Answer: Thread Priority in Java:

In Java, each thread has a priority.


The JVM uses thread priority to decide which thread to run first (not
guaranteed, but hints to the scheduler).

By: Aaryan Waghmare and Chat GPT


PPL UNIT 5
1. setPriority(int n) Method

 Used to set the priority of a thread.


 Priority value must be between 1 (MIN) and 10 (MAX).
 Default priority is 5 (NORM_PRIORITY).

Syntax: thread.setPriority(7);

2. getPriority() Method

 Returns the priority of a thread.

Syntax: int p = thread.getPriority();

Example:

class MyThread extends Thread {


public void run() {
System.out.println("Priority: " +
getPriority());
}
}

public class Test {


public static void main(String[] args) {
MyThread t = new MyThread();
t.setPriority(8); // set priority
t.start(); // prints: Priority: 8
}
}

Q.6) State the term thread synchronization. Explain how to


achieve thread synchronization in Java. [6 Marks]
Answer: Thread synchronization is a mechanism that ensures that only one thread can
access a shared resource (like a variable, method, or object) at a time.

It is used to prevent data inconsistency and race conditions in multithreaded programs.

By: Aaryan Waghmare and Chat GPT


PPL UNIT 5
Java provides the synchronized keyword to achieve thread synchronization

🔹 1. Synchronized Method

class Table {
synchronized void print() {
for (int i = 1; i <= 3; i++)
System.out.println(i);
}
}

🔹 2. Synchronized Block

class Table {
void print() {
synchronized (this) {
for (int i = 1; i <= 3; i++)
System.out.println(i);
}
}
}

Example:

class Table {
synchronized void print() {
for (int i = 1; i <= 3; i++)
System.out.println(i);
}
}

class MyThread extends Thread {


Table t;
MyThread(Table t) { this.t = t; }
public void run() { t.print(); }
}

public class Test {


public static void main(String[] args) {
Table t = new Table();
new MyThread(t).start();
new MyThread(t).start();
}
}

By: Aaryan Waghmare and Chat GPT


PPL UNIT 5
Q.7) Compare React.js, Angular.js, and Vue.js. Also state there
limitations and advantages.
Answer:
Feature React.js Angular.js Vue.js
Full framework
Lightweight
Type Library (only for UI) (handles
framework
everything)
Developed By Facebook Google Evan You
Learning Hard (many Easy and beginner-
Easy to medium
Curve concepts) friendly
Data Binding One-way Two-way Two-way
Fast (uses Virtual Slower in large Fast (Virtual DOM
Performance
DOM) apps used)
Size Small Large Very small
✅ Complete
✅ Reusable ✅ Simple syntax
solution
Advantages components
✅ Two-way
✅ Fast rendering ✅ Easy to integrate
binding
❌ Complex
❌ Only handles UI ❌ Smaller ecosystem
Limitations ❌ Heavy for
❌ JSX can confuse ❌ Less enterprise use
small apps

Q.8) List the features of JavaScript and write a JavaScript


program to display Welcome message. [6 Marks]
Answer: Features of JavaScript:
1. Lightweight & Fast – Runs directly in the browser.
2. Interpreted Language – No need for compilation.
3. Object-Based – Supports objects like window, document, etc.
4. Event-Driven – Responds to user actions like clicks, input, etc.
5. Client-Side Scripting – Runs on the user's browser, reducing server load.
6. Cross-platform – Works on all browsers and operating systems.

By: Aaryan Waghmare and Chat GPT


PPL UNIT 5
JavaScript Program to Display Welcome Message:

<!DOCTYPE html>
<html>
<body>
<script>
alert("Welcome to JavaScript!");
</script>
</body>
</html>

Output:

When the page is opened in a browser, a popup alert box shows:


"Welcome to JavaScript!"

Q.9) Write a JavaScript program to develop a simple web


application? [9]

Answer:

By: Aaryan Waghmare and Chat GPT


PPL UNIT 5
1. HTML Structure
o <!DOCTYPE html> declares HTML5 standard
o <head> contains metadata and CSS styling
o <body> holds visible content (heading, counter display, button)
2. JavaScript Logic
o let count = 0 initializes the counter
o onclick="increment()" attaches event handler
o increment() function updates both variable and DOM
3. Styling & Best Practices
o Basic CSS for visual appeal (centering, button styling)
o Semantic variable/function naming (count, increment())
o Proper DOM manipulation using getElementById

By: Aaryan Waghmare and Chat GPT

You might also like