Closed zhamri closed 6 years ago
Source Code
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package mythread;
public class Counter extends Counter{
public static final int TOTAL_THREADS = 50;
public final static Object obj = new Object();
int threadNo;
static volatile int counter = 100;
public Counter(int threadNo){
this.threadNo= threadNo;
}
@Override
public void run(){
//in a synchronized block to acquire lock
synchronized (obj) {
while(counter<=1000){
if(counter == threadNo || (counter%TOTAL_THREADS == threadNo) ||
((counter%TOTAL_THREADS == 0) && (TOTAL_THREADS == threadNo))){
if (counter%5==0){
System.out.println("Thread Printing"+" "+counter);
}
if (counter==555){
counter=1001;
System.out.println("System closing....");
}
counter++;
//notify
obj.notifyAll();
}else{
//current thread not eligible for printing the current counter value, so wait till its notified
try {
obj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
public static void main (String args[]) {
/*
* Creating as many threads as needed.
*/
for(int i = 1; i<=TOTAL_THREADS;i++){
Counter th = new Counter(i);
th.start();
}
}
}
Screenshot
Source code
public class Issue11 {
public static void main(String args[]) {
new ThreadTest(" ").start();
}
}
class ThreadTest extends Thread {
public ThreadTest(String str) {
super(str);
}
public void run() {
int i = 0;
for(i=100;i<=1000;i++) {
if(i%10==5) {
System.out.print(i+" ");
}
if(i%10==5)
System.out.println();
if(i==555){
break;
}
}
}
}
Output
Source code
Main Class
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.stiw3054.issue10;
/**
*
* @author Saufi
*/
public class Main extends Thread{
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws InterruptedException {
Sub runnable = new Sub();
Thread t1 = new Thread (runnable);
t1.start();
}
}
Sub Class
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.stiw3054.issue10;
public class Sub implements Runnable {
@Override
synchronized public void run() {
for(int x=100;x<=1000;x++){
if (x % 10 == 5) {
System.out.println(x);
if (x==555){
break;
}
}
try
{
Thread.sleep(10);
}
catch (InterruptedException e)
{}
}
}
}
package com.mycompany.issue_11;
public class threadNum extends Thread {
@Override
public void run() {
int count = 1;
for (int i = 100; i <= 1000; i++) {
if (i % 10 == 5) {
System.out.print((count++ % 10 != 0) ? i + " " : i + "\n");
}
if (i == 555) {
break;
}
}
}
public static void main(String[] args) {
threadNum thread = new threadNum();
thread.start();
}
}
Code
package issue10;
import java.util.concurrent.locks.ReentrantLock;
public class ThreadsExample implements Runnable {
static int counter = 100;
static ReentrantLock counterLock = new ReentrantLock(true); // enable fairness policy
@Override
public void run() {
while (counter < 1000) {
counterLock.lock();
// Always good practice to enclose locks in a try-finally block
try {
if (counter % 5 == 0) {
if (counter % 10 == 5) {
System.out.println(Thread.currentThread().getName() + ": " + counter);
}
if (counter == 555) {
counter = 555 * 2;
//break;
}
}
counter++;
} finally {
counterLock.unlock();
}
}
}
public static void main(String[] args) {
ThreadsExample te = new ThreadsExample();
Thread thread1 = new Thread(te);
Thread thread2 = new Thread(te);
Thread thread3 = new Thread(te);
Thread thread4 = new Thread(te);
thread1.start();
thread2.start();
thread3.start();
thread4.start();
}
}
Output
public class Exercise10 extends Thread {
public static void main(String[] args) {
Exercise10 t1 = new Exercise10();
t1.count();
}
public void count() {
for (int i = 100; i <= 1000; i++) {
if (i % 10 == 5) {
System.out.println(i);
}
if (i == 555) {
break;
}
}
}
}
import java.util.concurrent.Semaphore;
/**
*
* @author NUTSYABA
*/
public class issue10 extends Thread {
public int name;
final Value v;
final Semaphore lock;
public issue10(Value v, Semaphore lock) {
this.v = v;
this.lock = lock;
}
public void run() {
for (v.i = 100; v.i <= 1000; v.i++) {
if (v.i % 10 == 5) {
System.out.println(v.i);
if (v.i == 555) {
break;
}
}
}
}
public static void main(String[] args) {
Value v = new Value();
Semaphore lock = new Semaphore(1);
issue10 a = new issue10(v, lock);
a.name = 1;
a.start();
}
static class Value {
int i = 1;
}
}
package exercise10;
public class Exercise10 {
public static void main(String args[]) {
new ThreadTest(" ").start();
}
}
class ThreadTest extends Thread {
public ThreadTest(String str) {
super(str);
}
public void run() {
int i = 0;
for(i=100;i<=1000;i++) {
if(i%10==5) {
System.out.print(i+" ");
}
if(i%10==5)
System.out.println();
if(i==555){
System.out.println("System stoped...");
break;
}
}
}
}
public class Issues10 extends Thread {
public static void main(String[] args) {
Issues10 thread = new Issues10();
thread.start();
}
public void run() {
for (int i = 100; i <= 1000; i++) {
if (i % 5 == 0) {
System.out.println(i);
}
if (i == 555) {
break;
}
}
}
}
JAVA CODE
public class exercise10 {
public static void main(String[] args) {
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 100; i < 565; i++) {
if (i % 10 == 5) {
System.out.println("Number 5 : " + i);
}
}
}
});
t1.start();
}
}
RESULTS
Exercise10.java
package exercise10;
public class Exercise10 extends Thread {
@Override
public void run() {
int num = 1;
for (int i = 100; i <= 1000; i++) {
if (i % 10 == 5) {
System.out.print((num++ % 10 != 0) ? i + " " : i + "\n");
}
if (i == 555) {
System.out.println();
System.out.println("Stopping all Threads !");
break;
}
}
}
public static void main(String[] args) {
Exercise10 thread = new Exercise10();
thread.start();
}
}
Output :
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package issues;
/**
*
* @author abdullah
*/
public class Issue10 {
public static void main(String[] args) {
Thread count = new Thread(new NumberClass() ," Multiples of 5 From 100 - 555");
count.start();
}
}
class NumberClass implements Runnable{
@Override
public void run() {
Thread t = Thread.currentThread();
int count = 0;
System.out.println("Count started: " + t.getName());
try {
Thread.sleep(4000);
for (int i=100; i<1000; i++){
//System.out.println(i);
if (i % 10 == 5) {
System.out.print(i+"");
System.out.print((++count % 10 == 0) ? "\n" : " " );
}
if (i == 555)
break;
;
}
}
catch (InterruptedException ie) {
ie.printStackTrace();
}
System.out.println("\nThread ended: "+t.getName() +"\n");
}
}
package issue.pkg11;
public class Issue11 extends Thread {
public static void main(String[] args) {
new Thread(new Issue11()).start();
}
public void run() {
for (int i = 100; i <= 1000; i++) {
if (i % 10 == 5) {
System.out.println(i);
}
if (i == 555) {
break;
}
}
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.mycompany.issue11;
/**
*
* @author Alif Haikal
*/
public class Issue11 extends Thread {
public static void main(String[] args) {
Issue11 t1 = new Issue11();
t1.count();
}
public void count() {
for (int i = 100; i <= 1000; i++) {
if (i % 10 == 5) {
System.out.println(i);
}
if (i == 555) {
break;
}
}
}
}
package issue11;
/**
*
* @author esp
*/
public class Issue11 extends Thread {
public static void main(String[] args) {
Issue11 t1 = new Issue11();
t1.count();
}
public void count() {
for (int i = 100; i < 565; i++) {
if (i % 10 == 5) {
System.out.println(i);
}
if (i == 555) {
System.out.println("Stopped !");
break;
}
}
}
}
Write a Java program using threads to count numbers from 100 to 1000 which consist of number “5”. If the number is “555” then stop all the threads.