Skip to main content

Posts

Showing posts with the label Java MultiThread Programming

Write a Java program Design a screen with two buttons start thread and stop thread. Clicking on start it should start printing "Thread running" until stop button is pressed.

Write a Java program Design a screen with two buttons start thread and stop thread. Clicking on start it should start printing "Thread running" until stop button is pressed. Program: import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ThreadRun extends JFrame implements Runnable, ActionListener { int i,j; String str, msg = "Thread running"; Thread t; JButton btnStart, btnStop; JPanel panel; public ThreadRun() { Container c = getContentPane(); setDefaultCloseOperation(EXIT_ON_CLOSE); t = new Thread(this); panel = new JPanel(); btnStart = new JButton("Start Thread"); btnStop = new JButton("Stop Thread"); panel.add(btnStart); panel.add(btnStop); btnStart.addActionListener(this); btnStop.addActionListener(this); c.add(panel); setSize(500,600); setVisible(true); j=8...

Write a Java program to display "Hello Java" 50 times using multithreading.

Write a Java program to display "Hello Java" 50 times using multithreading. Program: import java.lang.*; class HelloThread extends Thread { public HelloThread(String name) { super(name); } public void run() { try { for(int i=0;i<25;i++){ System.out.println(Thread.currentThread().getName()+"Hello"); sleep(500); } } catch(InterruptedException ie){ System.out.println(ie); } } } public class ThreadMain { public static void main(String args[]){ HelloThread t = new HelloThread("My Thread"); t.start(); try{ for(int i=0;i<25;i++){ System.out.println(Thread.currentThread().getName()+"Hello"); Thread.sleep(500); } } catch(InterruptedException e) { System.out.println(e); } } }

Write a Java program which will generate threads to display 10 terms of Fibonacci Series and to display 1 to 20 in Reverse order.

Write a Java program which will generate threads to display 10 terms of Fibonacci Series and to display 1 to 20 in Reverse order. Program: import java.lang.*; class FibonacciReverse extends Thread{ String str; FibonacciReverse(String s){ str = s; setName(str); start(); } public void run() { int a=1,b=1,i,j; int c=a+b; try { for(i=0;i<=10;i++) { a=b; b=c; c=a+b; System.out.println(c); sleep(500); } } catch(InterruptedException e) { System.out.println(e); } try { for(j=20;j>=1;j--) { System.out.println(j); sleep(500); } } catch (InterruptedException e){ System.out.println(e); } } } class ThreadMain { public static void main(String args[]){ FibonacciReverse t1 = new Fibonacc...

Write a Java program which will create two child threads by implementing Runnable interface. One thread will print even numbers from 1 to 50 and other display vowels.

Write a Java program which will create two child threads by implementing Runnable interface. One thread will print even numbers from 1 to 50 and other display vowels. Program: import java.io.*; import java.util.*; public class EvenNumberVowel implements Runnable { String name; Thread t; EvenNumberVowel(String str) { name = str; t = new Thread(this, name); t.start(); } public void run() { name = Thread.currentThread().getName(); if(name.equals("First")) { for(int i=1;i<=50;i++){ try { if(i%2==0) { System.out.println("First:"+i); t.sleep(500); } } catch(InterruptedException ie) { System.out.println(ie); } } } else if(name.equals("Second")) { char vowels[] = {'A','E','I...

Write a Java program to accept number from user and Calculate factorial of given number and Also check whether given number is prime or not. (Use Thread)

Write a Java program to accept number from user and Calculate factorial of given number and Also check whether given number is prime or not. (Use Thread) Program: import java.io.*; import java.lang.*; class FactPrime extends Thread { String str; int num1, num2, fact=1, flag=1, i; FactPrime(String s, int n) { num1 = n; str = s; setName(str); start(); } public void run() { if(str.equals("First")) { try { for(i=1; i<=num1; i++) { fact = fact*i; sleep(500); } System.out.println("Factorial: "+fact); } catch (InterruptedException e) { System.out.println(e); } } else if(str.equals("Second")) { try { for(i=2;i<num1;i++) { if(num1%i==0){ flag = 0; bre...