0% found this document useful (0 votes)
23 views

Java Lab 8

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)
23 views

Java Lab 8

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
You are on page 1/ 6

Omar Mukhtar University Faculty of Engineering

Dept. of Computer Engineering


Albeida , Libya
Instructor: Mrs. Krishna Kumari Ganga
Advanced Programming 1

1. A Demo Program on Threads


class Thread2 extends Thread {
public void run() {
for (int i = 1; i < 5; i++) {
System.out.println("From Thread A I=: " + i);
try{
sleep(1000);
}catch(Exception e){}
}
System.out.println("Exit from thread A");
}
}

class Thread3 extends Thread {


public void run() {
for (int i = 1; i < 5; i++) {
System.out.println("From Thread B J=: " + i);
try{
sleep(1000);
}catch(Exception e){}
}

System.out.println("Exit from thread B");


}
}

class Thread4 extends Thread {


public void run() {
for (int i = 1; i < 5; i++) {
System.out.println("From Thread C K=: " + i);
try{
sleep(1000);
}catch(Exception e){}
}

System.out.println("Exit from thread C");


}
}
public class Thread1 {
public static void main(String[] args) {
Thread2 A = new Thread2();
Thread3 B = new Thread3();
Thread4 C = new Thread4();
C.setPriority(Thread.MAX_PRIORITY);
Advanced Programming Using Java - Prepared by: Krishna Kumari Ganga
B.setPriority(A.getPriority()+1);
A.setPriority(Thread.MIN_PRIORITY);
System.out.println("Start Thread A");
A.start();
System.out.println("Start Thread B");
B.start();
System.out.println("Start Thread C");
C.start();
System.out.println("Exit from main Thread");
}
}

2. Write a program to print the following output using multi-thread concept


Hello Student
Welcome
Hello Student
Welcome
Hello Student
Welcome
Hello Student
Welcome
Hello Student
Welcome

public class ThreadDemo {


public static void main(String[] args) {
A obj1 = new A();
obj1.start();
B obj2 = new B();
obj2.start();
}
}
class A extends Thread {
public void show() {
for (int i = 0; i < 5; i++) {
System.out.println("Hello Student");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
}
public void run() {
show();
}
}
class B extends Thread {
public void show() {
Advanced Programming Using Java - Prepared by: Krishna Kumari Ganga
for (int i = 0; i < 5; i++) {
System.out.println("Welcome");
try {
Thread.sleep(1000);
} catch (Exception e) {
}
}
}
public void run() {
show();
}
}

3. Write a program to print the following output using multi-thread concept by


using both extending from a Thread class and implementing Runnable Interface
Hello
Hai
Hello
Hai
Hello
Hai
Hello
Hai
Hello
Hai

public class Display {


public static void main(String[] args) {
A110 A = new A110();
A.start();
B1 B=new B1();
Thread t=new Thread(B);
t.start();
}
}
class A1 extends Thread {
public void run(){
for (int i = 0; i < 5; i++) {
System.out.println("Hello ");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
}
}
class B1 implements Runnable {
public void run(){
Advanced Programming Using Java - Prepared by: Krishna Kumari Ganga
for (int i = 0; i < 5; i++) {
System.out.println("Hai ");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
(OR)
public class TreadDemo {
public static void main(String[] args) {
A11 a = new A101("Hai");
A11 b = new A101("Hello");
Thread t1=new Thread(a);
t1.start();
Thread t2=new Thread(b);
t2.start();

}
}
class A11 implements Runnable {
String msg;
A11(String m){
msg=m;
}
public void run(){
for (int i = 0; i < 5; i++) {
System.out.println(msg);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
}
}
4. Using Lambda Expression
public class DemoLambda {
public static void main(String[] args) {
A111 A = new A111();
A.start();
Thread t=new Thread(()->{
for (int i = 0; i < 5; i++) {
System.out.println("Hai OMU Libya");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Advanced Programming Using Java - Prepared by: Krishna Kumari Ganga
e.printStackTrace();
}
}
}
);
t.start();
}
}
class A111 extends Thread {
public void run(){
for (int i = 0; i < 5; i++) {
System.out.println("Hai OMU");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
}
}
5. Example program on inter-thread communication using piped input stream class and
piped output stream class
import java.io.*;
import java.util.*;
public class PipeTest {

public static void main(String[] args) {


try {
PipedOutputStream pos=new PipedOutputStream();
PipedInputStream pis=new PipedInputStream(pos);
Numgen ng=new Numgen(pos);
Runavg rg=new Runavg(pis);
ng.start();
rg.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class Numgen extends Thread{
DataOutputStream dos;
Numgen(PipedOutputStream pos){
try {
dos=new DataOutputStream (pos);
} catch (Exception e) {
e.printStackTrace();
}
}
public void run(){
Advanced Programming Using Java - Prepared by: Krishna Kumari Ganga
try {
Random ran=new Random();
double d;
while(true){
d=ran.nextFloat()*1000;
dos.writeDouble(d);
dos.flush();
}
}catch (IOException e) {
e.printStackTrace();
}
}
}
class Runavg extends Thread{
DataInputStream dis;
Runavg(PipedInputStream pis){
try {
dis=new DataInputStream (pis);
} catch (Exception e) {
e.printStackTrace();
}
}
public void run(){
try {
double total=0;
int counter=0;
while(true){
total=total+dis.readDouble();
counter++;
System.out.println("Average of the Random generated numbers
from Thread 1 is :"+total/counter);
}
}catch (IOException e) {
e.printStackTrace();
}
}
}

Best of luck

Advanced Programming Using Java - Prepared by: Krishna Kumari Ganga

You might also like