Open zhamri opened 4 years ago
class counter
{
int count = 0;
public synchronized void increament()
{
count++;
}
}
public class Synchronized
{
public static void main (String [] args) throws Exception
{
counter c = new counter();
Thread t1 = new Thread(new Runnable() {
@Override
public void run()
{
for(int i=2;i<10;i++)
{
System.out.println(i);
c.increament();
}
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public synchronized void run() {
for(int i=2;i<10;i++)
{
System.out.println(i);
c.increament();
}
}
});
Thread t3 = new Thread(new Runnable() {
@Override
public synchronized void run() {
for(int i=2;i<10;i++)
{
System.out.println(i);
c.increament();
}
}
});
t1.start();
t2.start();
t1.join();
t2.join();
t3.start();
t3.join();
System.out.println("count = " + c.count);
}
}
/*
* 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 Week_06;
//import static java.lang.Thread.sleep;
/**
*
* @author MICKY
*/
class Thread1 extends Thread {
@Override
public void run() {
try {
for (int i = 1; i <= 10; i++) {
synchronized (this){
switch(i){
case 5:
System.out.print("");
break;
case 6:
System.out.print("");
break;
case 7:
System.out.print("");
break;
}
System.out.println(i);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
class Thread2 extends Thread {
@Override
public void run() {
try {
for (int i = 1; i <= 10; i++) {
synchronized (this){
switch(i){
case 5:
System.out.print("");
break;
case 6:
System.out.print("");
break;
case 7:
System.out.print("");
break;
}
System.out.println(i);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
class Thread3 extends Thread {
@Override
public void run() {
try {
for (int i = 1; i <= 10; i++) {
synchronized (this){
switch(i){
case 5:
System.out.print("");
break;
case 6:
System.out.print("");
break;
case 7:
System.out.print("");
break;
}
System.out.println(i);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class issueEight {
public static synchronized void main(String[] args) {
Thread1 t1 = new Thread1();
Thread2 t2= new Thread2();
Thread3 t3= new Thread3();
t1.start();
t2.start();
t3.start();
}
}
package realtimeExercise;
//instruction
//Write a Java program using THREE (3) threads.
//Each thread will display numbers between 1 and 10.
//However, number 5,6,7 must be protected from interleaving among threads.
public class sychronizedNumber extends Thread {
public static void main(String[] args) throws InterruptedException {
MyThread();
}
public static void MyThread() throws InterruptedException {
Thread t1 = new Thread() {
public void run() {
for (int i=1;i<=10;i++) {
synchronized (this){
switch(i){
case 5:
System.out.print("Lock ");
break;
case 6:
System.out.print("Lock ");
break;
case 7:
System.out.print("Lock ");
break;
}
}
System.out.println("The current number is:"+ i);
}
}
};
Thread t2 = new Thread() {
public void run() {
for (int i=1;i<=10;i++) {
synchronized (this){
switch(i){
case 5:
System.out.print("Lock ");
break;
case 6:
System.out.print("Lock ");
break;
case 7:
System.out.print("Lock ");
break;
}
}
System.out.println("The current number is:"+ i);
}
}
};
Thread t3 = new Thread() {
public void run() {
for (int i=1;i<=10;i++) {
synchronized (this){
switch(i){
case 5:
System.out.print("Lock ");
break;
case 6:
System.out.print("Lock ");
break;
case 7:
System.out.print("Lock ");
break;
}
}
System.out.println("The current number is:"+ i);
}
}
};
t1.start();
t1.join();
t2.start();
t2.join();
t3.start();
}
public class Sync {
public static void Thread() throws InterruptedException{
Thread T1 = new Thread(){
public void run(){
for (int i = 1;i<=10; i++){
System.out.println(i);
}//forA
}//run
};//T1
Thread T2 = new Thread(){
public void run(){
for (int i = 1;i<=10; i++){
System.out.println(i);
}//forB
}//run
};//T2
Thread T3 = new Thread(){
public void run(){
for (int i = 1;i<=10; i++){
System.out.println(i);
}//forC
}//run
};//T3
T1.start();
T1.join();//wait until T1 finish
T2.start();
T2.join();//wait until T2 finish
T3.start();
}//Thread()
public static void main(String [] args) throws InterruptedException{
Thread();
}//main
//reference taken from https://github.com/zhamri/STIW3054-RT-Programming/blob/master/src/Week_06/TestSynchronized2s1.java
}//Sync
/*
* 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 Thread05;
/**
*
* @author WANG LUO
*/
class Thread1 extends Thread {
@Override
public void run() {
try {
for (int i = 1; i <= 10; i++) {
synchronized (this){
switch(i){
case 5:
System.out.print("Lock: ");
break;
case 6:
System.out.print("Lock: ");
break;
case 7:
System.out.print("Lock: ");
break;
}
System.out.println(i);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
class Thread2 extends Thread {
@Override
public void run() {
try {
for (int i = 1; i <= 10; i++) {
synchronized (this){
switch(i){
case 5:
System.out.print("Lock: ");
break;
case 6:
System.out.print("Lock: ");
break;
case 7:
System.out.print("Lock: ");
break;
}
System.out.println(i);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
class Thread3 extends Thread {
@Override
public void run() {
try {
for (int i = 1; i <= 10; i++) {
synchronized (this){
switch(i){
case 5:
System.out.print("Lock: ");
break;
case 6:
System.out.print("Lock: ");
break;
case 7:
System.out.print("Lock: ");
break;
}
System.out.println(i);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
class issueEight {
public static synchronized void main(String[] args) throws InterruptedException {
Thread1 t1 = new Thread1();
Thread2 t2= new Thread2();
Thread3 t3= new Thread3();
t1.start();
t1.join();
t2.start();
t2.join();
t3.start();
}
}
public static void main(String[] args) throws InterruptedException {
MyThread();
}
public static void MyThread() throws InterruptedException { Thread t1 = new Thread() { public void run() { for (int i=1;i<=10;i++) { synchronized (this){ switch(i){ case 5: System.out.print("Lock "); break; case 6: System.out.print("Lock "); break; case 7: System.out.print("Lock "); break; } } System.out.println("The current number is:"+ i); } } }; Thread t2 = new Thread() { public void run() { for (int i=1;i<=10;i++) { synchronized (this){
switch(i){
case 5:
System.out.print("Lock ");
break;
case 6:
System.out.print("Lock ");
break;
case 7:
System.out.print("Lock ");
break;
}
}
System.out.println("The current number is:"+ i);
}
}
};
Thread t3 = new Thread() {
public void run() {
for (int i=1;i<=10;i++) {
synchronized (this){
switch(i){
case 5:
System.out.print("Lock ");
break;
case 6:
System.out.print("Lock ");
break;
case 7:
System.out.print("Lock ");
break;
}
}
System.out.println("The current number is:"+ i);
}
}
};
t1.start();
t1.join();
t2.start();
t2.join();
t3.start();
Tan Cheng Yi 253814
public class issue8 {
public static void main(String args[]){
count t1 =new count("Thread 1");
count t2 =new count("Thread 2");
count t3 =new count("Thread 3");
t1.start();
t2.start();
t3.start();
}
}
class count extends Thread{
count(String name){
super(name);
}
@Override
public void run() {
for(int i=1;i<11;i++) {
switch(i){
case 5:
synchronized (this) {
for(int j=5;j<=7;j++)
System.out.println(getName()+": "+j);
}
i=7;
break;
default:
System.out.println(getName()+": "+i);
}
}
}
}
package threads;
/**
*
* @author Chee Fung
*/
class setNumber1 implements Runnable {
public void run(){
for(int i = 1; i <= 10; i++){
System.out.println("thread number one "+i);
Thread.yield();
try {
Thread.sleep(1000);
}catch(Exception e){}
}
}
}
class setNumber2 implements Runnable {
public void run(){
for(int i = 1; i <= 10; i++){
System.out.println("thread number two "+i);
Thread.yield();
try {
Thread.sleep(1000);
}catch(Exception e){}
}
}
}
class setNumber3 implements Runnable {
public void run(){
for(int i = 1; i <= 10; i++){
System.out.println("thread number three "+i);
Thread.yield();
try {
Thread.sleep(1000);
}catch(Exception e){}
}
}
}
public class Main_Issues_6 {
public static void main(String[] args) throws Exception{
Thread t1 = new Thread(new setNumber1());
Thread t2 = new Thread(new setNumber2());
Thread t3 = new Thread(new setNumber3());
t1.start();
t1.join();
t2.start();
t2.join();
t3.start();
t3.join();
}
}
public class ThreadSync {
public static void MyThread() throws InterruptedException
{
Thread t1 = new Thread()
{
public void run()
{
for (int i = 1; i <= 10; i++)
{
synchronized (this)
{
switch (i)
{
case 5:
case 7:
case 6:
System.out.print("Lock ");
break;
}
}
System.out.println(i);
}
}
};
Thread t2 = new Thread()
{
public void run()
{
for (int i = 1; i <= 10; i++)
{
synchronized (this)
{
switch (i)
{
case 5:
case 7:
case 6:
System.out.print("Lock ");
break;
}
}
System.out.println(i);
}
}
};
Thread t3 = new Thread()
{
public void run()
{
for (int i = 1; i <= 10; i++)
{
synchronized (this)
{
switch (i)
{
case 5:
case 7:
case 6:
System.out.print("Lock ");
break;
}
}
System.out.println(i);
}
}
};
t1.start();
t1.join();
t2.start();
t2.join();
t3.start();
}
public static void main(String[] args) throws InterruptedException
{
MyThread();
}
}
package Synchronized;
public class Count {
public static void main(String args[]) throws InterruptedException {
run t1 =new run();
run t2 =new run();
run t3 =new run();
t1.start();
t1.join();
t2.start();
t2.join();
t3.start();
t3.join();
}
}
class run extends Thread{
@Override
public void run() {
synchronized (this){
for(int i = 1;i <= 10;i++){
switch (i)
{
case 5:
case 7:
case 6:
System.out.print("Lock Number: ");
break;
}
System.out.println("Current Number : " + i);
}
}
}
}
package com.xunsheng;
class Threads extends Thread {
public int[] array = new int[10];
public static int getRandomInteger(int max, int min){
return ((int) (Math.random()*(max - min))) + min;
}
public void run(){
for(int i = 0; i<=array.length-1; i++){
array[i] = getRandomInteger(10,1);
}
}
}
class Verifier {
public boolean verify(int a, int b, int c){
if((5 <= a && a <=7) && (5 <= b && b <=7) && (5 <= c && c <=7)) {
return false;
}
else{
return true;
}
}
}
public class Main {
public static void main(String[] args) {
// write your code here
Verifier v = new Verifier();
Threads t1 = new Threads();
t1.setName("Thread 1");
Threads t2 = new Threads();
t2.setName("Thread 2");
Threads t3 = new Threads();
t3.setName("Thread 3");
t1.start();
t2.start();
t3.start();
for(int i = 0; i<10;i++){
Boolean tf = v.verify(t1.array[i],t2.array[i],t3.array[i]);
if(tf){
System.out.println(t1.getName() + ": " + t1.array[i]+"\n" +
t2.getName() + ": " + t2.array[i]+"\n" +
t3.getName() + ": " + t3.array[i]+"\n");
}
}
}
}
package real_time_programming;
public class Issue_8 {
public static void main(String[] args) throws InterruptedException {
Thread3 thd1 = new Thread3();
Thread3 thd2 = new Thread3();
Thread3 thd3 = new Thread3();
thd1.start();
thd1.join();
thd2.start();
thd2.join();
thd3.start();
thd3.join();
}
}
class Thread3 extends Thread{
@Override
public void run() {
synchronized (this){
for(int i = 1;i <= 10;i++){
switch(i){
case 5:
System.out.print("This is number : ");
break;
case 6:
System.out.print("This is number : ");
break;
case 7:
System.out.print("This is number : ");
break;
}
System.out.println(i);
}
}
}
}
class Thread1 extends Thread {
@Override
public void run() {
for (int i = 1; i <= 10; i++) {
synchronized (this){
switch(i){
case 5:
System.out.print("Lock number ");
break;
case 6:
System.out.print("Lock number ");
break;
case 7:
System.out.print("Lock number ");
break;
}
System.out.println(i);
}
}
}
}
class Thread2 extends Thread {
@Override
public void run() {
for (int i = 1; i <= 10; i++) {
synchronized (this){
switch(i){
case 5:
System.out.print("Lock number ");
break;
case 6:
System.out.print("Lock number ");
break;
case 7:
System.out.print("Lock number ");
break;
}
System.out.println(i);
}
}
}
}
class Thread3 extends Thread {
@Override
public void run() {
for (int i = 1; i <= 10; i++) {
synchronized (this){
switch(i){
case 5:
System.out.print("Lock number ");
break;
case 6:
System.out.print("Lock number ");
break;
case 7:
System.out.print("Lock number ");
break;
}
System.out.println(i);
}
}
}
}
class main {
public static void main(String[] args) throws Exception{
Thread1 t1 = new Thread1();
Thread2 t2= new Thread2();
Thread3 t3= new Thread3();
t1.start();
t2.start();
t1.join();
t2.join();
t3.start();
t3.join();
}
}
TAN WEI DI 259296
public class Synchronized extends Thread {
public static void main(String[] args) {
Synchronized myclass = new Synchronized();
Thread thread1 = new Thread(myclass);
Thread thread2 = new Thread(myclass);
Thread thread3 = new Thread(myclass);
thread1.setName("thread1");
thread2.setName("thread2");
thread3.setName("thread3");
thread1.start();
thread2.start();
thread3.start();
}
public void run(){
for(int i=1;i<10;i++) {
if(i==5){
synchronized(this){
for (int j = 5; j < 8; j++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("\u001B[33m"+Thread.currentThread().getName() + " : " + j +"\u001B[0m");
}
}
i=7;
}else {
System.out.println(Thread.currentThread().getName() + " : " + i);
}
}
}
}
package week6;
public class numbering {
public static void main(String args[]) throws InterruptedException {
run t1 =new run();
run t2 =new run();
run t3 =new run();
t1.start();
t2.start();
t3.start();
}
} class run extends Thread{ @Override public void run() { synchronized (this){ for(int i = 1;i <= 10;i++){ switch (i) { case 5: case 7: case 6: System.out.print("Lock number: "); break; } System.out.println(i); }
}
}
}
public class threadClass {
public static void main(String [] args) throws InterruptedException{
Thread();
}
public static void Thread() throws InterruptedException{
Thread T1 = new Thread(){
public void run(){
for (int i = 1;i<=10; i++){
switch (i)
{
case 5:
case 7:
case 6:
System.out.print("protect : ");
break;
}
System.out.println(" " + i);
}
}
};
Thread T2 = new Thread(){
public void run(){
for (int i = 1;i<=10; i++){
switch (i)
{
case 5:
case 7:
case 6:
System.out.print("protect : ");
break;
}
System.out.println(" " + i);
}
}
};
Thread T3 = new Thread(){
public void run(){
for (int i = 1;i<=10; i++){
switch (i)
{
case 5:
case 7:
case 6:
System.out.print("protect : ");
break;
}
System.out.println(" " + i);
}
}
};
T1.start();
T1.join();
T2.start();
T2.join();
T3.start();
T3.join();
}
}
//Mohd Fazlizam Bin Ahmad Puad (259132)
public class sychronizedNumber extends Thread {
public static void main(String[] args) throws InterruptedException {
MyThread();
}
public static void MyThread() throws InterruptedException {
Thread t1 = new Thread() {
@Override
public void run() {
for (int i=1;i<=10;i++) {
synchronized (this){
switch(i){
case 5:
System.out.print("");
break;
case 6:
System.out.print("");
break;
case 7:
System.out.print("");
break;
}
}
System.out.println(i);
}
}
};
Thread t2 = new Thread() {
@Override
public void run() {
for (int i=1;i<=10;i++) {
synchronized (this){
switch(i){
case 5:
System.out.print("");
break;
case 6:
System.out.print("");
break;
case 7:
System.out.print("");
break;
}
}
System.out.println(i);
}
}
};
Thread t3 = new Thread() {
@Override
public void run() {
for (int i=1;i<=10;i++) {
synchronized (this){
switch(i){
case 5:
System.out.print("");
break;
case 6:
System.out.print("");
break;
case 7:
System.out.print("");
break;
}
}
System.out.println(i);
}
}
};
t1.start();
t1.join();
t2.start();
t2.join();
t3.start();
t3.join();
}
}
public class mainFlow
{
public static void main(String[] args)
{
firstThread justThread = new firstThread();
Thread firstThread = new Thread(justThread);
Thread secondThread = new Thread(justThread);
Thread thirdThread = new Thread(justThread);
firstThread.setName("Thread-1");
secondThread.setName("Thread-2");
thirdThread.setName("Thread-3");
firstThread.start();
secondThread.start();
thirdThread.start();
}
}
public class firstThread extends Thread
{
private static final String ANSI_PURPLE = "\u001B[35m";
private static final String ANSI_RESET = "\u001B[0m";
public void run()
{
for (int k = 1; k < 10; k++)
{
if (k == 5)
{
synchronized (this)
{
for (int x = 5; x < 8; x++)
{
try
{
Thread.sleep(500);
} catch (InterruptedException ie)
{
ie.printStackTrace();
}
System.out.println(ANSI_PURPLE + "Locked: "
+ Thread.currentThread().getName() + ":" + x + ANSI_RESET);
}
}
}
else if (k <= 4 || k >= 8)
{
System.out.println(Thread.currentThread().getName() + ":" + k);
}
}
}
}
package Issue8;
public class Issue8 {
private static Thread tone = new thread_1();
private static Thread ttwo = new thread_2();
private static Thread tthree = new thread_3();
public static void main(String[] args) throws InterruptedException {
tone.setName("tone");
ttwo.setName("ttwo");
tthree.setName("tthree");
tone.start();
ttwo.start();
tthree.start();
}
}
public class thread_1 extends Thread{
static int tNo1 = 1;
static final String PURPLE = "\u001B[36m";
static final String BLANK = "\u001B[0m";
public void run() {
for (tNo1 = 1; tNo1 <= 10; tNo1++) {
if (tNo1 >= 5 && tNo1 <= 7) {
synchronized (this) {
System.out.println(PURPLE + "Protect1: " + tNo1 + BLANK);
}
} else{
System.out.println("T1: " + tNo1);
}
}
}
}
public class thread_2 extends Thread{
private static final String PURPLE = "\u001B[36m";
private static final String BLANK = "\u001B[0m";
static int tNo2 = 1;
public void run() {
for (tNo2 = 1; tNo2 <= 10; tNo2++) {
if (tNo2 >= 5 && tNo2 <= 7) {
synchronized (this) {
System.out.println(PURPLE + "Protect2: " + tNo2 + BLANK);
}
} else{
System.out.println("T2: " + tNo2);
}
}
}
}
public class thread_3 extends Thread{
private static final String PURPLE = "\u001B[36m";
private static final String BLANK = "\u001B[0m";
static int tNo3 = 1;
public void run() {
for (tNo3 = 1; tNo3 <= 10; tNo3++) {
if (tNo3 >= 5 && tNo3 <= 7) {
synchronized (this) {
System.out.println(PURPLE + "Protect3: " + tNo3 + BLANK);
}
} else{
System.out.println("T3: " + tNo3);
}
}
}
}
public class class1
{
int count = 0;
public synchronized void increament()
{
count++;
}
}
////////////////////////////////////////////////////////////
public class class2 {
public static void main (String [] args) throws Exception
{
class1 a = new class1();
Thread t1 = new Thread(new Runnable() {
@Override
public void run()
{
for(int i=1;i<=10;i++)
{
System.out.println(i);
a.increament();
}
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public synchronized void run() {
for(int i=1;i<=10;i++)
{
System.out.println(i);
a.increament();
}
}
});
Thread t3 = new Thread(new Runnable() {
@Override
public synchronized void run() {
for(int i=1;i<=10;i++)
{
System.out.println(i);
a.increament();
}
}
});
t1.start();
t2.start();
t1.join();
t2.join();
t3.start();
t3.join();
System.out.println("Total = " + a.count);
}
}
`
class thread1 extends Thread {
@Override
public void run(){
try {
for (int i = 1; i < 11; i++) {
synchronized (this) {
switch (i) {
case 5:
System.out.print("Number: ");
break;
case 6:
System.out.print("Number: ");
break;
case 7:
System.out.print("Number: ");
break;
}
System.out.println(i);
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
class thread2 extends Thread {
public void run(){
try{
for (int i = 1; i < 11; i++) {
synchronized (this) {
switch (i) {
case 5:
System.out.print("Number: ");
break;
case 6:
System.out.print("Number: ");
break;
case 7:
System.out.print("Number: ");
break;
}
System.out.println(i);
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
class thread3 extends Thread {
@Override
public void run(){
try {
for (int i = 1; i < 11; i++) {
synchronized (this) {
switch (i) {
case 5:
System.out.print("Number: ");
break;
case 6:
System.out.print("Number: ");
break;
case 7:
System.out.print("Number: ");
break;
}
System.out.println(i);
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
public class Sync {
public static synchronized void main (String [] args) throws InterruptedException {
thread1 t1 = new thread1();
thread2 t2 = new thread2();
thread3 t3 = new thread3();
t1.start();
t1.join();
t2.start();
t2.join();
t3.start();
t3.join();
}
}
public class MyjavaInter {
static int number1=0;
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws InterruptedException {
// TODO code application logic here
Thread t1 = new threadA();
Thread t2 = new threadB();
Thread t3 = new threadC();
t1.start();
t1.join();
t2.start();
t2.join();
t3.start();
t3.join();
}
static class threadA extends Thread{
public void run(){
for (int number = 1; number<=10 ; number++){
if(number==5 || number ==6||number ==7) {
synchronized (this){
System.out.println("Thread A Lock : "+number);
}
}else{
System.out.println("Thread A : "+number);
}
}
}
}
static class threadB extends Thread{
public void run(){
for (int number = 1; number<=10 ; number++){
if(number==5 || number ==6||number ==7) {
synchronized (this){
System.out.println("Thread B Lock : "+number);
}
}else{
System.out.println("Thread B : "+number);
}
}}
}
static class threadC extends Thread{
public void run(){
for (int number = 1; number<=10 ; number++){
if(number==5 || number ==6||number ==7) {
synchronized (this){
System.out.println("Thread C Lock : "+number);
}
}else{
System.out.println("Thread C : "+number);
}}
}
}
}
package com.company;
class Synchronized extends Thread{
public void run(){
synchronized (this) {
for (int i = 1; i <= 10; i++) {
System.out.println(i);
}
}
}
public static void main(String[] args){
Synchronized ts = new Synchronized( );
Thread t1 = new Thread(ts);
Thread t2 = new Thread(ts);
Thread t3 = new Thread(ts);
t1.start();
t2.start();
t3.start();
}
}
/*
* 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 sychronized;
/**
*
* @author yeong
*/
public class Sychronized {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws InterruptedException {
Thread1 t1 = new Thread1();
Thread2 t2= new Thread2();
Thread3 t3= new Thread3();
t1.start();
t1.join();
t2.start();
t2.join();
t3.start();
}
}
class Thread1 extends Thread {
@Override
public void run() {
try {
for (int x = 1; x <= 10; x++) {
synchronized (this){
switch(x){
case 5:
System.out.print("LOCK: ");
break;
case 6:
System.out.print("LOCK: ");
break;
case 7:
System.out.print("LOCK: ");
break;
}
System.out.println(x);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
class Thread2 extends Thread {
@Override
public void run() {
try {
for (int i = 1; i <= 10; i++) {
synchronized (this){
switch(i){
case 5:
System.out.print("LOCK: ");
break;
case 6:
System.out.print("LOCK: ");
break;
case 7:
System.out.print("LOCK: ");
break;
}
System.out.println(i);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
class Thread3 extends Thread {
@Override
public void run() {
try {
for (int i = 1; i <= 10; i++) {
synchronized (this){
switch(i){
case 5:
System.out.print("LOCK: ");
break;
case 6:
System.out.print("LOCK: ");
break;
case 7:
System.out.print("LOCK: ");
break;
}
System.out.println(i);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package issue;
/**
*
* @author Asus
*/
public class Issue {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Thread0 t0 = new Thread0();
Thread1 t1 = new Thread1();
Thread2 t2 = new Thread2();
t0.start();
t1.start();
t2.start();
}
}
class Thread0 extends Thread {
@Override
public void run(){
try{
for(int j = 1;j<=10;j++){
synchronized(this){
switch(j){
case 5:
System.out.print("");
break;
case 6:
System.out.print("");
break;
case 7:
System.out.print("");
break;
}
System.out.println(j);
}
}
}catch(Exception e){
e.printStackTrace();
}
}
}
class Thread1 extends Thread {
@Override
public void run(){
try{
for(int j = 1;j<=10;j++){
synchronized(this){
switch(j){
case 5:
System.out.print("");
break;
case 6:
System.out.print("");
break;
case 7:
System.out.print("");
break;
}
System.out.println(j);
}
}
}catch(Exception e){
e.printStackTrace();
}
}
}
class Thread2 extends Thread {
@Override
public void run(){
try{
for(int j = 1;j<=10;j++){
synchronized(this){
switch(j){
case 5:
System.out.print("");
break;
case 6:
System.out.print("");
break;
case 7:
System.out.print("");
break;
}
System.out.println(j);
}
}
}catch(Exception e){
e.printStackTrace();
}
}
}
public class thread {
public static void main(String[] args) throws InterruptedException {
a t1 = new a();
b t2 = new b();
c t3 = new c();
t1.start();
t1.join();
t2.start();
t2.join();
t3.start();
t3.join();
}
}
class a extends Thread {
public void run() {
try {
for (int i = 1; i <= 10; i++) {
System.out.println(i);
switch (i)
{
case 5:
case 7:
case 6:
System.out.print("Lock: ");
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
class b extends Thread {
public void run() {
try {
for (int i = 1; i <= 10; i++) {
System.out.println(i);
switch (i)
{
case 5:
case 7:
case 6:
System.out.print("Lock: ");
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
class c extends Thread {
public void run() {
try {
for (int i = 1; i <= 10; i++) {
System.out.println(i);
switch (i)
{
case 5:
case 7:
case 6:
System.out.print("Lock: ");
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package ThreadTest3;
public class TreadTest3_1 extends Thread {
@Override
public void run(){
try{
for(int i = 1;i <= 10;i ++){
synchronized (this){
switch (i){
case 5:
System.out.print("Protect");
break;
case 6:
System.out.print("Protect");
break;
case 7:
System.out.print("Protect");
break;
}
}
System.out.println(" The current number is "+i);
}
} catch (Exception e){
e.printStackTrace();
};
}
}
class ThreadTest3_2 extends Thread{
@Override
public void run(){
try{
for(int i = 1;i <= 10;i ++){
synchronized (this){
switch (i){
case 5:
System.out.print("Protect");
break;
case 6:
System.out.print("Protect");
break;
case 7:
System.out.print("Protect");
break;
}
}
System.out.println(" The current number is "+i);
}
} catch (Exception e){
e.printStackTrace();
};
}
}
class ThreadTest3_3 extends Thread{
@Override
public void run(){
try{
for(int i = 1;i <= 10;i ++){
synchronized (this){
switch (i){
case 5:
System.out.print("Protect");
break;
case 6:
System.out.print("Protect");
break;
case 7:
System.out.print("Protect");
break;
}
}
System.out.println(" The current number is "+i);
}
} catch (Exception e){
e.printStackTrace();
};
}
}
class ThreadTest3_0 {
public static synchronized void main(String[] args) throws InterruptedException {
TreadTest3_1 t1 = new TreadTest3_1();
t1.start();
t1.join();
ThreadTest3_2 t2= new ThreadTest3_2();
t2.start();
t2.join();
ThreadTest3_3 t3= new ThreadTest3_3();
t3.start();
}
}
/*
/*
public class thread {
public static void main(String[] args) throws InterruptedException {
one x1 = new one();
two z2 = new two();
three y3 = new three();
x1.start();
x1.join();
y3.start();
y3.join();
z2.start();
z2.join();
}
}
class one extends Thread {
public void run() {
try {
for (int a = 1; a <= 10; a++) {
System.out.println(a);
switch (a)
{
case 5:
case 7:
case 6:
System.out.print("LOCK = ");
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
class two extends Thread {
public void run() {
try {
for (int a = 1; a <= 10; a++) {
System.out.println(a);
switch (a)
{
case 5:
case 7:
case 6:
System.out.print("LOCK = ");
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
class three extends Thread {
public void run() {
try {
for (int a = 1; a <= 10; a++) {
System.out.println(a);
switch (a)
{
case 5:
case 7:
case 6:
System.out.print("LOCK = ");
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
matric number:255403
code:
package javaapplication40;
/*
@author Nora98 */ class firstThread extends Thread{
@Override public void run(){
for (int a=1; a<=10; a++){
synchronized (this){
switch (a) {
case 5:
case 6:
case 7:
System.out.print("num: ");
break;
}
System.out.println(a);
}
}
}
}
package javaapplication40;
/*
@author Nora98 */ class secondThread extends Thread { @Override public void run(){
for (int a=1; a<=10; a++){
synchronized (this){
switch (a) {
case 5:
case 6:
case 7:
System.out.print("num: ");
break;
}
System.out.println(a);
}
}
} }
package javaapplication40;
/*
@author Nora98 */ class thirdThread extends Thread{ @Override public void run(){
for (int a=1; a<=10; a++){
synchronized (this){
switch (a) {
case 5:
case 6:
case 7:
System.out.print("num: ");
break;
}
System.out.println(a);
}
}
} }
package javaapplication40;
/*
@author Nora98 */ public class total { public static synchronized void main (String [] args) throws InterruptedException{
firstThread t1 = new firstThread();
secondThread t2 = new secondThread();
thirdThread t3 = new thirdThread();
t1.start();
t1.join();
t2.start();
t2.join();
t3.start();
t3.join();
} }
/*
/*
@author Ahmed_Bawazir */ public class Theard {
/**
@param args the command line arguments */ public static void main(String[] args)throws InterruptedException { // TODO code application logic here
Theard1 a = new Theard1(); Thread2 a2=new Thread2(); Thread3 a3=new Thread3(); a.start(); a2.start(); a3.start();
}
}
package theard;
/*
@author Ahmed_Bawazir */ public class Theard1 extends Thread {
@Override public void run() {
for (int i = 1; i < 11; i++) {
synchronized (this) {
switch (i) {
case 5:
System.out.print("lock:");
break;
case 6:
System.out.print("lock:");
break;
case 7:
System.out.print("lock:");
break;
}
}
System.out.println(i);
}
} }
package theard;
/*
@author Ahmed_Bawazir */ public class Thread2 extends Thread { @Override public void run(){
for(int i=1;i<=10;i++){
synchronized (this) {
switch (i) {
case 5:
System.out.print("lock:");
break;
case 6:
System.out.print("lock:");
break;
case 7:
System.out.print("lock:");
break;
}
}
System.out.println(i);
}
}
}
package theard;
/*
@author Ahmed_Bawazir */ public class Thread3 extends Thread {
@Override public void run() {
for (int i = 1; i <= 10; i++) {
synchronized (this) {
switch (i) {
case 5:
System.out.print("lock:");
break;
case 6:
System.out.print("lock:");
break;
case 7:
System.out.print("lock:");
break;
}
}
System.out.println(i);
}
} }
public class synnum {
final static int NUMBERS_IN_SEQUENCE = 10;
final static int NUMBER_OF_THREADS = 3;
public static void main(String[] args) {
synout sp = new synout(NUMBERS_IN_SEQUENCE, NUMBER_OF_THREADS);
Thread t1 = new Thread(new synrun(sp, 1), "Thread1");
Thread t2 = new Thread(new synrun(sp, 2), "Thread2");
Thread t3 = new Thread(new synrun(sp, 0), "Thread3");
t1.start();
t2.start();
t3.start();
}
}
class synrun implements Runnable{
synout sp;
int result;
static Object sharedObj = new Object();
synrun(synout sp, int result){
this.sp = sp;
this.result = result;
}
@Override
public void run() {
sp.printNum(result);
}
}
class synout{
int number = 1;
int numOfThreads;
int numInSequence;
synout(int numInSequence, int numOfThreads){
this.numInSequence = numInSequence;
this.numOfThreads = numOfThreads;
}
public void printNum(int result){
synchronized(this) {
while (number < numInSequence - 1) {
while(number % numOfThreads != result){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName() + " - " + number++);
this.notifyAll();
}
}
}
}
/*
/*
@author Maccik */ public class Threadsnum extends Thread {
public static void main(String[] args) {
Threadsnum myclass = new Threadsnum();
Thread thread1 = new Thread(myclass);
Thread thread2 = new Thread(myclass);
Thread thread3 = new Thread(myclass);
thread1.setName("Thread 1");
thread2.setName("Thread 2");
thread3.setName("Thread 3");
thread1.start();
thread2.start();
thread3.start();
}
public void run(){ for(int i=1;i<10;i++) {
if(i==5){
synchronized(this){
for (int j = 5; j < 8; j++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("\u001B[33m"+Thread.currentThread().getName() + " : " + j +"\u001B[0m");
}
}
i=7;
}else {
System.out.println(Thread.currentThread().getName() + " : " + i);
}
}
}
}
import java.util.concurrent.locks.ReentrantLock;
class Thread1 extends Thread {
private final ReentrantLock lock = new ReentrantLock();
@Override
public void run() {
lock.lock();
try {
for (int i = 1; i <= 10; i++) {
switch(i){
case 5:
System.out.print("Lock:");
break;
case 6:
System.out.print("Lock:");
break;
case 7:
System.out.print("Lock:");
break;
}
System.out.println(i);
}
} catch (Exception e) {
lock.unlock();
}
}
}
class Thread2 extends Thread {
private final ReentrantLock lock = new ReentrantLock();
@Override
public void run() {
lock.lock();
try {
for (int i = 1; i <= 10; i++) {
switch(i){
case 5:
System.out.print("Lock:");
break;
case 6:
System.out.print("Lock:");
break;
case 7:
System.out.print("Lock:");
break;
}
System.out.println(i);
}
} catch (Exception e) {
lock.unlock();
}
}
}
class Thread3 extends Thread {
private final ReentrantLock lock = new ReentrantLock();
@Override
public void run() {
lock.lock();
try {
for (int i = 1; i <= 10; i++) {
switch(i){
case 5:
System.out.print("Lock:");
break;
case 6:
System.out.print("Lock:");
break;
case 7:
System.out.print("Lock:");
break;
}
System.out.println(i);
}
} catch (Exception e) {
lock.unlock();
}
}
}
public class Main {
public static void main(String[] args) {
Thread1 t1 = new Thread1();
Thread2 t2= new Thread2();
Thread3 t3= new Thread3();
t1.start();
t2.start();
t3.start();
}
}
public class Synchronized
{
public static void MyThread() throws InterruptedException
{
Thread t1 = new Thread()
{
public void run()
{
for (int i = 1; i <= 10; i++)
{
synchronized (this)
{
switch (i)
{
case 5:
case 7:
case 6:
System.out.print("Lock ");
break;
}
}
System.out.println(i);
}
}
};
Thread t2 = new Thread()
{
public void run()
{
for (int i = 1; i <= 10; i++)
{
synchronized (this)
{
switch (i)
{
case 5:
case 7:
case 6:
System.out.print("Lock ");
break;
}
}
System.out.println(i);
}
}
};
Thread t3 = new Thread()
{
public void run()
{
for (int i = 1; i <= 10; i++)
{
synchronized (this)
{
switch (i)
{
case 5:
case 7:
case 6:
System.out.print("Lock ");
break;
}
}
System.out.println(i);
}
}
};
t1.start();
t1.join();
t2.start();
t2.join();
t3.start();
}
public static void main(String[] args) throws InterruptedException
{
MyThread();
}
}
Instruction
Write a Java program using THREE (3) threads. Each thread will display numbers between 1 and 10. However, number 5,6,7 must be protected from interleaving among threads.
Submission