Sei sulla pagina 1di 15

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();
System.out.println("from thread A : i:"+i);
}
System.out.println("exit from A ");
}
}
class thr2 extends Thread
{
@Override
public void run()
{
for(int i=0;i<5;i++){
System.out.println("from thread B : i:"+i);
if(i==3)
stop();
}
System.out.println("exit from B ");
}
}
class thr3 extends Thread
{
@Override
public void run()
{
for(int i=0;i<5;i++){
System.out.println("from thread C : i:"+i);
if(i==2)
{
try
{
sleep(1000);
}
catch(Exception a)
{
System.out.println("Eror");
}
}
}
System.out.println("exit from C ");
}
}
public class Thread1 {
public static void main(String[] args) {
thr1 a1 = new thr1();
thr2 b1 = new thr2();
thr3 c1 = new thr3();
System.out.println("Stsrt from A ");
a1.start();
System.out.println("Stsrt from B ");
b1.start();
System.out.println("Stsrt from C ");
c1.start();

}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.
EX.NO. 12
ARRAY LIST
23/09/19

AIM:
To develop a Java program to perform string operations using array list.Write functions
for
1.Append - add at end
2.Insert - add at particular index
3.Search
4.List all strings starting with given letter
5.List all strings ending with given letter

SOURCE CODE:
package javaapplication39;
import java.util.ArrayList;
import java.util.Scanner;
public class JavaApplication39 {
public static void main(String[] args) {
int ch=1,choice,ind;
Scanner obj=new Scanner(System.in);
ArrayList <String> list=new ArrayList <String> ();
do{
System.out.print("ARRAY
LIST\n==================\n1.Append\n2.Insert\n3.Search\n4.Start with\n5.End
with\nEnter your choice : ");
choice=obj.nextInt();
switch(choice){
case 1:
System.out.print("Enter the string : ");
obj.nextLine();
list.add(obj.nextLine());
System.out.println("The list is : "+list);
break;
case 2:
System.out.print("Enter the index : ");
ind=obj.nextInt();
System.out.print("Enter the string : ");
obj.nextLine();
list.add(ind,obj.nextLine());
System.out.println("The list is : "+list);
break;
case 3:
System.out.print("Enter the string : ");
obj.nextLine();
System.out.println("Element found at index : "+list.indexOf(obj.nextLine()));
break;
case 4:
System.out.print("Enter the start char : ");
String v=obj.next();
System.out.print("Desired strings : ");
for(String i:list)
{
if(i.startsWith(v))
{
System.out.print(i+" ");
}
}
System.out.println();
break;
case 5:
System.out.print("Enter the end char : ");
String j=obj.next();
System.out.print("Desired strings : ");
for(String i:list)
{
if(i.endsWith(j))
{
System.out.print(i+" ");
}
}
System.out.println();
break;
}
System.out.print("Press 1 to continue : ");
ch=obj.nextInt();
}while(ch==1);
}
}
OUTPUT :
ARRAY LIST
==================
1.Append
2.Insert
3.Search
4.Start with
5.End with
Enter your choice : 1
Enter the string : dhoni
The list is : [dhoni]
Press 1 to continue : 1
ARRAY LIST
==================
1.Append
2.Insert
3.Search
4.Start with
5.End withS
Enter your choice : 1
Enter the string : virat
The list is : [dhoni, virat]
Press 1 to continue : 1
ARRAY LIST
==================
1.Append
2.Insert
3.Search
4.Start with
5.End 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
==================
1.Append
2.Insert
3.Search
4.Start with
5.End with
Enter your choice : 3
Enter the string : virat
Element found at index : 2
Press 1 to continue : 1
ARRAY LIST
==================
1.Append
2.Insert
3.Search
4.Start with
5.End with
Enter your choice : 4
Enter the start char : s
Desired strings : sachin
Press 1 to continue : 1
ARRAY LIST
==================
1.Append
2.Insert
3.Search
4.Start with
5.End 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.
EX.NO. 13
FILE OPERATIONS IN JAVA
23/09/19

AIM:
To develop a Java program to implement file input output using
1.FileReader
2.FileWriter
3.FileInputStream
4.FileOutputStream
5.RandomAccessFile

SOURCE CODE:

1.FileWriter
======================
package javaapplication34;
import java.util.*;
import java.io.*;
public class JavaApplication34 {
public static void main(String[] args) {
String name,rollno,dept;
Scanner obj=new Scanner(System.in);
try{
System.out.print("Enter the name : ");
name=obj.nextLine();
System.out.print("Enter the roll no : ");
rollno=obj.nextLine();
System.out.print("Enter the dept : ");
dept=obj.nextLine();
FileWriter outf=new FileWriter("D:\\eg.txt",true);
outf.write(name+"\n"+rollno+"\n"+dept+"\n");
System.out.println("Content written successfully...");
outf.close();
}
catch(IOException e)
{
System.out.println("Error occurred...");
}
}
}
Output:
Enter the name : Dhoni
Enter the roll no : 7
Enter the dept : CSE
Content written successfully...

eg.txt
Dhoni7CSE

2.FileReader
======================
package javaapplication35;
import java.io.FileReader;
import java.io.IOException;

public class JavaApplication35 {


public static void main(String[] args) {
try{
FileReader inf=new FileReader("D:\\eg.txt");
int temp;
while((temp=inf.read())!=-1)
{
System.out.print((char)temp);
}
System.out.println("Content read successfully...");
inf.close();
}
catch(IOException e)
{
System.out.println("Error occurred...");
}
}
}
Output:
Dhoni
7
CSE
Content read successfully...

3.FileOutputStream
=====================
package javaapplication37;

import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

public class JavaApplication37 {


public static void main(String[] args)
{
String name,rollno,dept;
Scanner obj=new Scanner(System.in);
try{
System.out.print("Enter the name : ");
name=obj.nextLine();
System.out.print("Enter the roll no : ");
rollno=obj.nextLine();
System.out.print("Enter the dept : ");
dept=obj.nextLine();
FileOutputStream outf=new FileOutputStream("D:\\eg.txt",true);
DataOutputStream dos=new DataOutputStream(outf);
dos.writeBytes(name+"\n"+rollno+"\n"+dept+"\n");
System.out.println("Content written successfully...");
outf.close();
}
catch(IOException e)
{
System.out.println("Error occurred...");
}
}
}

Output:
Enter the name : Virat
Enter the roll no : 18
Enter the dept : ECE
Content written successfully...

eg.txt
Dhoni7CSEVirat18ECE

4.FileInputStream
====================
package javaapplication36;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;

public class JavaApplication36 {


public static void main(String[] args) {
try{
FileInputStream inf=new FileInputStream("D:\\eg.txt");
Scanner input=new Scanner(inf);
while(input.hasNextLine())
{
System.out.println(input.nextLine());
}
System.out.println("Content read successfully...");
inf.close();
}
catch(IOException e)
{
System.out.println("Error occurred...");
}
}
}

Output:
Dhoni
7
CSE
Virat
18
ECE
Content read successfully...

5.RandomAccessFile
=======================
package javaapplication38;

import java.io.IOException;
import java.io.RandomAccessFile;

public class JavaApplication38 {


public static void main(String[] args) {
try{
RandomAccessFile rf=new RandomAccessFile("D:\\eg.txt","rw");
rf.writeUTF("DHONI");
rf.writeInt(07);
rf.writeChar('d');
rf.writeDouble(7.007);
rf.seek(0);

System.out.println(rf.readUTF()+"\n"+rf.readInt()+"\n"+rf.readChar()+"\n"+rf.readDouble());
rf.close();
}
catch(IOException e)
{
System.out.println("Error occurred...");
}
}
}

eg.txt
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 java.util.*;

class even implements Runnable {

public int x;

public even(int x) {
this.x = x;
}

public void run() {


System.out.println("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() {


System.out.println("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 = r.nextInt(100);
System.out.println("Main Thread and Generated Number is " + num);
if (num % 2 == 0) {
Thread t1 = new Thread(new even(num));
t1.start();
} else {
Thread t2 = new Thread(new odd(num));
t2.start();
}
Thread.sleep(1000);
System.out.println("--------------------------------------");
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}

public class javaApplication15 {

public static void main(String[] args) {


A a = new A();
a.start();
}
}
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.

Potrebbero piacerti anche