Ex no: Threads in java
Date :
Aim:
To write a java program that implements a thread and perform the operations of yield
,sleep,stop in multithreading.
Source Code:
package yieldstopsleep;
class thr1 extends Thread
{
@Override
public void run()
{
for(int i=0;i<5;i++){
if(i==4)
yield();
[Link]("from thread A : i:"+i);
}
[Link]("exit from A ");
}
}
class thr2 extends Thread
{
@Override
public void run()
{
for(int i=0;i<5;i++){
[Link]("from thread B : i:"+i);
if(i==3)
stop();
}
[Link]("exit from B ");
}
}
class thr3 extends Thread
{
@Override
public void run()
{
for(int i=0;i<5;i++){
[Link]("from thread C : i:"+i);
if(i==2)
{
try
{
sleep(1000);
}
catch(Exception a)
{
[Link]("Eror");
}
}
}
[Link]("exit from C ");
}
}
public class Thread1 {
public static void main(String[] args) {
thr1 a1 = new thr1();
thr2 b1 = new thr2();
thr3 c1 = new thr3();
[Link]("Stsrt from A ");
[Link]();
[Link]("Stsrt from B ");
[Link]();
[Link]("Stsrt from C ");
[Link]();
}Learning Outcome:
1. Implementation of thread
2. operations of:
Yield
Stop
Sleep
Output:
run:
Stsrt from A
Stsrt from B
Stsrt from C
from thread A : i:0
from thread A : i:1
from thread A : i:2
from thread A : i:3
from thread A : i:4
from thread C : i:0
from thread C : i:1
from thread C : i:2
from thread B : i:0
from thread B : i:1
from thread B : i:2
from thread B : i:3
exit from A
from thread C : i:3
from thread C : i:4
exit from C
BUILD SUCCESSFUL (total time: 1 second)
Result:
Thus a java program that implemented and executed.
[Link]. 12
ARRAY LIST
23/09/19
AIM:
To develop a Java program to perform string operations using array [Link] functions
for
[Link] - add at end
[Link] - add at particular index
[Link]
[Link] all strings starting with given letter
[Link] all strings ending with given letter
SOURCE CODE:
package javaapplication39;
import [Link];
import [Link];
public class JavaApplication39 {
public static void main(String[] args) {
int ch=1,choice,ind;
Scanner obj=new Scanner([Link]);
ArrayList <String> list=new ArrayList <String> ();
do{
[Link]("ARRAY
LIST\n==================\[Link]\[Link]\[Link]\[Link] with\[Link]
with\nEnter your choice : ");
choice=[Link]();
switch(choice){
case 1:
[Link]("Enter the string : ");
[Link]();
[Link]([Link]());
[Link]("The list is : "+list);
break;
case 2:
[Link]("Enter the index : ");
ind=[Link]();
[Link]("Enter the string : ");
[Link]();
[Link](ind,[Link]());
[Link]("The list is : "+list);
break;
case 3:
[Link]("Enter the string : ");
[Link]();
[Link]("Element found at index : "+[Link]([Link]()));
break;
case 4:
[Link]("Enter the start char : ");
String v=[Link]();
[Link]("Desired strings : ");
for(String i:list)
{
if([Link](v))
{
[Link](i+" ");
}
}
[Link]();
break;
case 5:
[Link]("Enter the end char : ");
String j=[Link]();
[Link]("Desired strings : ");
for(String i:list)
{
if([Link](j))
{
[Link](i+" ");
}
}
[Link]();
break;
}
[Link]("Press 1 to continue : ");
ch=[Link]();
}while(ch==1);
}
}
OUTPUT :
ARRAY LIST
==================
[Link]
[Link]
[Link]
[Link] with
[Link] with
Enter your choice : 1
Enter the string : dhoni
The list is : [dhoni]
Press 1 to continue : 1
ARRAY LIST
==================
[Link]
[Link]
[Link]
[Link] with
[Link] withS
Enter your choice : 1
Enter the string : virat
The list is : [dhoni, virat]
Press 1 to continue : 1
ARRAY LIST
==================
[Link]
[Link]
[Link]
[Link] with
[Link] with
Enter your choice : 2
Enter the index : 1
Enter the string : sachin
The list is : [dhoni, sachin, virat]
Press 1 to continue : 1
ARRAY LIST
==================
[Link]
[Link]
[Link]
[Link] with
[Link] with
Enter your choice : 3
Enter the string : virat
Element found at index : 2
Press 1 to continue : 1
ARRAY LIST
==================
[Link]
[Link]
[Link]
[Link] with
[Link] with
Enter your choice : 4
Enter the start char : s
Desired strings : sachin
Press 1 to continue : 1
ARRAY LIST
==================
[Link]
[Link]
[Link]
[Link] with
[Link] with
Enter your choice : 5
Enter the end char : i
Desired strings : dhoni
Press 1 to continue : 0
LEARNING OUTCOMES:
Creating an Array list
Implementing various operations on Array List
RESULT:
Thus the java program to perform string operations using array list is written and executed
and the output is verified.
[Link]. 13
FILE OPERATIONS IN JAVA
23/09/19
AIM:
To develop a Java program to implement file input output using
[Link]
[Link]
[Link]
[Link]
[Link]
SOURCE CODE:
[Link]
======================
package javaapplication34;
import [Link].*;
import [Link].*;
public class JavaApplication34 {
public static void main(String[] args) {
String name,rollno,dept;
Scanner obj=new Scanner([Link]);
try{
[Link]("Enter the name : ");
name=[Link]();
[Link]("Enter the roll no : ");
rollno=[Link]();
[Link]("Enter the dept : ");
dept=[Link]();
FileWriter outf=new FileWriter("D:\\[Link]",true);
[Link](name+"\n"+rollno+"\n"+dept+"\n");
[Link]("Content written successfully...");
[Link]();
}
catch(IOException e)
{
[Link]("Error occurred...");
}
}
}
Output:
Enter the name : Dhoni
Enter the roll no : 7
Enter the dept : CSE
Content written successfully...
[Link]
Dhoni7CSE
[Link]
======================
package javaapplication35;
import [Link];
import [Link];
public class JavaApplication35 {
public static void main(String[] args) {
try{
FileReader inf=new FileReader("D:\\[Link]");
int temp;
while((temp=[Link]())!=-1)
{
[Link]((char)temp);
}
[Link]("Content read successfully...");
[Link]();
}
catch(IOException e)
{
[Link]("Error occurred...");
}
}
}
Output:
Dhoni
7
CSE
Content read successfully...
[Link]
=====================
package javaapplication37;
import [Link];
import [Link];
import [Link];
import [Link];
public class JavaApplication37 {
public static void main(String[] args)
{
String name,rollno,dept;
Scanner obj=new Scanner([Link]);
try{
[Link]("Enter the name : ");
name=[Link]();
[Link]("Enter the roll no : ");
rollno=[Link]();
[Link]("Enter the dept : ");
dept=[Link]();
FileOutputStream outf=new FileOutputStream("D:\\[Link]",true);
DataOutputStream dos=new DataOutputStream(outf);
[Link](name+"\n"+rollno+"\n"+dept+"\n");
[Link]("Content written successfully...");
[Link]();
}
catch(IOException e)
{
[Link]("Error occurred...");
}
}
}
Output:
Enter the name : Virat
Enter the roll no : 18
Enter the dept : ECE
Content written successfully...
[Link]
Dhoni7CSEVirat18ECE
[Link]
====================
package javaapplication36;
import [Link];
import [Link];
import [Link];
public class JavaApplication36 {
public static void main(String[] args) {
try{
FileInputStream inf=new FileInputStream("D:\\[Link]");
Scanner input=new Scanner(inf);
while([Link]())
{
[Link]([Link]());
}
[Link]("Content read successfully...");
[Link]();
}
catch(IOException e)
{
[Link]("Error occurred...");
}
}
}
Output:
Dhoni
7
CSE
Virat
18
ECE
Content read successfully...
[Link]
=======================
package javaapplication38;
import [Link];
import [Link];
public class JavaApplication38 {
public static void main(String[] args) {
try{
RandomAccessFile rf=new RandomAccessFile("D:\\[Link]","rw");
[Link]("DHONI");
[Link](07);
[Link]('d');
[Link](7.007);
[Link](0);
[Link]([Link]()+"\n"+[Link]()+"\n"+[Link]()+"\n"+[Link]());
[Link]();
}
catch(IOException e)
{
[Link]("Error occurred...");
}
}
}
[Link]
DHONI d@ + Iº
Output:
DHONI
7
d
7.007
LEARNING OUTCOMES:
File concepts
Reading and writing operations on file
Random Access file
RESULT:
Thus the java program to implement file input and output operations is written and
executed and the output is verified.
Ex no: Multithreading
Date :
Aim:
To write a java program that implements a multithread applications that has three threads,
first thread generates a random integer every second and if value is even,second thread computes
square or third thread will print the cube of a number.
Source Code:
import [Link].*;
class even implements Runnable {
public int x;
public even(int x) {
this.x = x;
}
public void run() {
[Link]("New Thread " + x + " is EVEN and Square of " + x + " is: " + x * x);
}
}
class odd implements Runnable {
public int x;
public odd(int x) {
this.x = x;
}
public void run() {
[Link]("New Thread " + x + " is ODD and Cube of " + x + " is: " + x * x * x);
}
}
class A extends Thread {
public void run() {
int num = 0;
Random r = new Random();
try {
for (int i = 0; i < 5; i++) {
num = [Link](100);
[Link]("Main Thread and Generated Number is " + num);
if (num % 2 == 0) {
Thread t1 = new Thread(new even(num));
[Link]();
} else {
Thread t2 = new Thread(new odd(num));
[Link]();
}
[Link](1000);
[Link]("--------------------------------------");
}
} catch (Exception ex) {
[Link]([Link]());
}
}
}
public class javaApplication15 {
public static void main(String[] args) {
A a = new A();
[Link]();
}
}
Learning Outcome:
1. Implementation of multithread
2. Application of multithread
Output:
Main Thread and Generated Number is 86
New Thread 86 is EVEN and Square of 86 is: 7396
--------------------------------------
Main Thread and Generated Number is 33
New Thread 33 is ODD and Cube of 33 is: 35937
--------------------------------------
Main Thread and Generated Number is 71
New Thread 71 is ODD and Cube of 71 is: 357911
--------------------------------------
Main Thread and Generated Number is 89
New Thread 89 is ODD and Cube of 89 is: 704969
--------------------------------------
Main Thread and Generated Number is 23
New Thread 23 is ODD and Cube of 23 is: 12167
Result:
Thus a java program that implements a multithread applications that has three threads, first
thread generates a random integer every second and if value is even,second thread computes
square or third thread will print the cube of a number is executed succesfully.