0% found this document useful (0 votes)
39 views9 pages

20BIT004 - Mariyam - Bharmal - Prac 7

Uploaded by

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

20BIT004 - Mariyam - Bharmal - Prac 7

Uploaded by

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

Name: Mariyam.

Bharmal UID:20BIT004
Sub: Java Date:29 /08/2021
Practical 7: Vectors and Multithreading

7a. Write a java program to implement the vectors.

Source code:

import java.util.*;

public class prac_7a {


public static void main(String args[]){
Scanner s = new Scanner(System.in);
Vector <String> vec = new Vector<String>(5);
String choice,element,del;
int num,count;

System.out.println("Enter number of elemnets to be entered: ");


num = s.nextInt();

System.out.println("Enter elemnets: ");


for(int i =0;i<num;i++){
choice = s.next();
vec.addElement(choice);
}

System.out.println("Size of the vector:"+vec.size());

System.out.println("Search for any element: ");


element = s.next();

if (vec.contains(element)){
System.out.println(element+" is present at index: "+
vec.indexOf(element));
}

else{
System.out.println(element+" is not present");
}

System.out.println("Enter number of elements to be added again: ");


count= s.nextInt();
System.out.println("Enter elemnets: ");
for(int i =0;i<count;i++){
choice = s.next();
vec.addElement(choice);
}

System.out.println("Enter the element to be deleted: ");


del = s.next();
if (vec.contains(del)){
vec.remove(del);
System.out.println(del+" has been deleted");
System.out.println("After deletion: "+vec);
}

else{
System.out.println(del+" is not present");
}

}
}

Output

7b. Write a java program to implement thread life cycle.


Source code:
public class prac_7c extends Thread {
public void run(){
System.out.println("Thread is running...");
}
public static void main(String args[]){
prac_7c tl = new prac_7c();
tl.start();
}
}

Output:

7c. Write a java program to implement multithreading.

Source code:
class Multithreading extends Thread{
Multithreading() {
super(“Odd Numbers”);
setPriority(1);
int I = getPriority();
System.out.println(i);
start();
}
public void run(){
try{
int n = 10;
for (int I = 1; I <= 5; i++) {
if (I % 2 != 0) {
System.out.println(“Odd Number:” +i);
Thread.sleep(1000);
}
}
}catch(InterruptedException e){
System.out.println(“Odd thread is Interrupted”); //here interruption is
not handled by catch
}

}
}
class even extends Thread
{
even(){
super(“Even Numbers”);
setPriority(3);
int I = getPriority();
start();
}
public void run(){
try
{
int n = 10;
for(int I = 1; I <= 5; i++)
{
if (I % 2 == 0)
{
System.out.println(“Even Number:” +i);
Thread.sleep(1000);
}
}
}catch(InterruptedException e) {
System.out.println(“Even thread is Interrupted”); //here interruption is
not handled by catch
}
}
}
class hello extends Thread{
hello(){
super(“Hello”);
setPriority(2);
int I = getPriority();
start();
}
public void run()
{
try{
for (int I = 0; I <= 5; i++){
System.out.println(“Hello”);
Thread.sleep(400);
}
}catch(InterruptedException e){
System.out.println(“Hello thread is Interrupted”); //here interruption is not
handled by catch
}
}
}

public class prac_7b{


public static void main(String args[])throws InterruptedException{
new Multithreading();
new hello();
new even();
}
}

Output:
Write ups:

You might also like