Open zhamri opened 4 years ago
public class DisplayWord extends Thread
{
public void run()
{
try
{
System.out.println("universiti utara malaysia sintok kedah malaysia");
sleep(1000);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
class Thread2 extends Thread
{
public void run()
{
try
{
String s1 = "universiti";
char[] c = s1.toCharArray();
System.out.println("universiti");
for(int i=1;i<=9;i++)
{
System.out.println(c[i]);
sleep(500);
}
System.out.println();
System.out.println("utara");
String s2 = "utara";
char[] c1 = s2.toCharArray();
for (int j=1;j<=4;j++){
System.out.println(c1[j]);
sleep(500);
}
System.out.println();
System.out.println("malaysia");
String s3 = "malaysia";
char[] c2 = s3.toCharArray();
for (int k=1;k<=7;k++){
System.out.println(c2[k]);
sleep(500);
}
System.out.println();
System.out.println("sintok");
String s4 = "sintok";
char[] c3 = s4.toCharArray();
for (int l=1;l<=5;l++){
System.out.println(c3[l]);
sleep(500);
}
System.out.println();
System.out.println("kedah");
String s5 = "kedah";
char[] c4 = s5.toCharArray();
for (int m=1;m<=4;m++){
System.out.println(c4[m]);
sleep(500);
}
System.out.println();
System.out.println("malaysia");
String s6 = "malaysia";
char[] c5 = s6.toCharArray();
for (int n=1;n<=7;n++){
System.out.println(c5[n]);
sleep(500);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
class Exercise3
{
public static void main(String args[])
{
DisplayWord t1=new DisplayWord();
Thread2 t2=new Thread2();
t1.start();
t2.start();
}
}
package Exercise;
public class CharacterDisplay extends Thread
{
public void run()
{
try
{
System.out.println("University Utara Malaysia Sintok Kedah Malaysia");
sleep(1000);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
class Exercise2 extends Thread
{
public void run() {
try {
int i;
String s1 = "University";
char[] c = s1.toCharArray();
for (i = 1; i < c.length; i++)
{
System.out.println(c[i]);
sleep(1000);
}
String s2 = "Utara";
char[] c2 = s2.toCharArray();
System.out.println(s2);
for (i = 1; i < c2.length; i++)
{
System.out.println(c2[i]);
sleep(1000);
}
String s3 = "Malaysia";
char[] c3 = s3.toCharArray();
System.out.println(s3);
for (i = 1; i < c3.length; i++)
{
System.out.println(c3[i]);
sleep(1000);
}
String s4 = "Sintok";
char[] c4 = s4.toCharArray();
System.out.println(s4);
for (i = 1; i < c4.length; i++)
{
System.out.println(c4[i]);
sleep(1000);
}
String s5 = "Kedah";
char[] c5 = s5.toCharArray();
System.out.println(s5);
for (i = 1; i < c5.length; i++)
{
System.out.println(c5[i]);
sleep(1000);
}
String s6 = "Malaysia";
char[] c6 = s6.toCharArray();
System.out.println(s6);
for (i = 1; i < c6.length; i++)
{
System.out.println(c6[i]);
sleep(1000);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
class Exercise3
{
public static void main(String args[])
{
CharacterDisplay t1=new CharacterDisplay();
Exercise2 t2=new Exercise2();
t1.start();
t2.start();
}
}
package exercise;
//259521 Soh chee fung
class Upper_String extends Thread{
public void run(){
try
{
System.out.println("University Utara Malaysia Sintok Kedah Malaysia");
sleep(1000);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
class Lower_String extends Thread{
public void run(){
String str1 = "Universiti";
char[] a = str1.toCharArray();
System.out.println(str1);
for(int i = 1; i <a.length; i++){
System.out.println(a[i]);
}
String str2 = "Utara";
char[] b = str2.toCharArray();
System.out.println(str2);
for(int i = 1; i <b.length; i++){
System.out.println(b[i]);
}
String str3 = "Malaysia";
char[] c = str3.toCharArray();
System.out.println(str3);
for(int i = 1; i <c.length; i++){
System.out.println(c[i]);
}
String str4 = "Sintok";
char[] d = str4.toCharArray();
System.out.println(str4);
for(int i = 1; i <d.length; i++){
System.out.println(d[i]);
}
String str5 = "Malaysia";
char[] f = str5.toCharArray();
System.out.println(str5);
for(int i = 1; i <f.length; i++){
System.out.println(f[i]);
}
String str6 = "Kedah";
char[] g = str6.toCharArray();
System.out.println(str6);
for(int i = 1; i <g.length; i++){
System.out.println(g[i]);
}
try{
Thread.sleep(1000);
}catch(Exception e){
e.printStackTrace();
}
}
}
public class Exercise {
public static void main(String[] args) {
Upper_String one = new Upper_String();
Lower_String two = new Lower_String();
one.start();
two.start();
}
}
package realtimeexercise;
public class RealTimeExercise {
public static void main(String[] args) throws Exception {
char ch;
String str = "Universiti Utara Malaysia Sintok Kedah Malaysia";
String[] word = str.split(" ");
for (int k = 0; k < word.length; k++) {
System.out.println(word[k]);
for (int i = 1; i < word[k].length(); i++) {
ch = word[k].charAt(i);
System.out.println(ch);
}
}
}
}
Java Source Code
public class Main {
public static void main(String[] args){
String str="Universiti Utara Malaysia Sintok Kedah Malaysia";
vertical v = new vertical(str);
horizontal h =new horizontal(str);
h.setPriority(Thread.MAX_PRIORITY);
v.setPriority(Thread.MIN_PRIORITY);
h.start();
v.start();
}
}
class vertical extends Thread{ String str; vertical(String str){ this.str=str; } @Override public void run() { String[] arrOfStr = str.split("\s"); for (int i = 0; i < arrOfStr.length; i++) { if (arrOfStr[i] != null && !"".equals(arrOfStr[i])) { int strLen = arrOfStr[i].length();
for (int j = 1; j < strLen; j++) {
System.out.println(arrOfStr[i].charAt(j));
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Thread.yield();
}
}
class horizontal extends Thread { boolean waiting = true; boolean ready = false; String str;
horizontal(String str) {
this.str = str;
}
@Override
public void run() {
String[] arrOfStr = str.split("\\s");
for (String a : arrOfStr) {
System.out.println(a);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Thread.yield();
}
}
2. Screenshot of output
![image](https://user-images.githubusercontent.com/46247836/66020191-d65e7080-e518-11e9-9f8f-863e2ee10d6e.png)
package Thread02;
/**
*
* @author WANG LUO
*/
public class printer01 extends Thread{
String one = "universiti";
String two = "utara";
String three = "malaysia";
String four = "sintok";
public void run(){
try{
System.out.println(one);
Thread.sleep(1000);
System.out.println(two);
Thread.sleep(1000);
System.out.println(three);
Thread.sleep(1000);
System.out.println(four);
Thread.sleep(1000);
}
catch(Exception e){
e.printStackTrace();
}
}
}
class printer02 extends Thread{
String one = "universiti";
String two = "utara";
String three = "malaysia";
String four = "sintok";
public void run(){
try{
for(int i = 1;i < one.length();i++){
char on = one.charAt(i);
System.out.println(on);
Thread.sleep(120);
}
for(int x = 1;x < two.length();x++){
char tw = two.charAt(x);
System.out.println(tw);
Thread.sleep(160);
}
for(int a = 1;a < three.length();a++){
char th = three.charAt(a);
System.out.println(th);
Thread.sleep(200);
}
for(int b = 1;b < four.length();b++){
char fo = four.charAt(b);
System.out.println(fo);
Thread.sleep(240);
}}
catch(Exception e){
e.printStackTrace();
}
}
}
public class printerApp {
public static void main(String[] args){
printer01 prin1 = new printer01();
printer02 prin2 = new printer02();
try{
prin1.start();
prin2.start();
}
catch(Exception e){
e.printStackTrace();
}
}
}
TAN WEI DI 259296
import java.util.stream.Stream;
class splitString extends Thread{
public void run() {
String myText = "Universiti Utara Malaysia Sintok Kedah Malaysia";
String[] splitted = myText.split(" ");
for(int j=0;j<Stream.of(splitted).count();j++) {
System.out.println(splitted[j]);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class splitWord extends Thread{
public void run() {
String myText = "Universiti Utara Malaysia Sintok Kedah Malaysia";
String[] splitted = myText.split(" ");
for(int j=0;j<Stream.of(splitted).count();j++) {
for (int i = 1; i < splitted[j].length(); i++) {
char c = splitted[j].charAt(i);
System.out.println(c);
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class test {
public static void main(String[] args){
splitString splitString = new splitString();
splitString.start();
splitWord splitWord = new splitWord();
splitWord.start();
}
}
public class JavaApplication5 {
/**
* @param args the command line arguments
*/
public static class thread1 extends Thread{
String a;
thread1(String d){
a = d;
}
@Override
public void run(){
int i=0;
int j = a.length()-1;
while(i<= j){
System.out.print(a.charAt(i));
if(i != -1)
i++;
}
System.out.println();
}
}
public static class thread2 extends Thread{
String b;
thread2(String e){
b = e;
}
@Override
public void run(){
int i=0;
int j = b.length()-1;
while(i<= j){
System.out.println(b.charAt(i));
if(i != -1)
i++;
}
}}
public static void main(String[] args) throws InterruptedException {
// TODO code application logic here
int u=0;
Scanner scan = new Scanner(System.in);
String c;
c=scan.nextLine();
String[] splited = c.split("\\s+");
int f = splited.length-1;
while(u<=f){
//Thread 1 is horizontal
thread1 t1 = new thread1(splited[u]);
t1.start();
Thread.sleep(1000);
//Thread 2 is horizontal
thread2 t2 = new thread2(splited[u]);
t2.start();
Thread.sleep(1000);
u++;
}
}
}
package com.company;
import java.util.Scanner;
class Word extends Thread {
public void run(){
String sentence = "Universiti Utara Malaysia Sintok Kedah Malaysia";
Scanner sc = new Scanner(sentence);
while (sc.hasNext()){
String word = sc.next();
System.out.println(word);
}
Thread.yield();
}
}
class Character extends Thread {
public void run(){
String sentence = "Universiti Utara Malaysia Sintok Kedah Malaysia";
Scanner sc = new Scanner(sentence);
while (sc.hasNext()){
String word = sc.next();
for(int i =1; i<word.length();i++){
System.out.println(word.charAt(i));
}
}
Thread.yield();
}
}
public class Main {
public static void main(String[] args) {
Word w = new Word();
Character c = new Character();
w.setPriority(Thread.MAX_PRIORITY);
c.setPriority(Thread.MIN_PRIORITY);
w.start();
c.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 exercise2;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author yeong
*/
public class Exercise2 {
static thread1 t1 = new thread1();
static thread2 t2 = new thread2();
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws InterruptedException {
t1.start();
t2.start();
}
}
class thread1 extends Thread{
public void run(){
try{
System.out.println("University");
Thread.sleep(1000);
System.out.println("Utara");
Thread.sleep(1000);
System.out.println("Malaysia");
Thread.sleep(1000);
System.out.println("Sintok");
Thread.sleep(1000);
}
catch(Exception e){
e.printStackTrace();
}
}
}
class thread2 extends Thread{
public void run(){
String Malaysia = "M a l a y s i a";
String Sintok = "S i n t o k";
String University = "U n i v e r s i t i";
String Utara = "U t a r a";
String[] part1 = University.split(" ");
String[] part2 = Utara.split(" ");
String[] part3 = Malaysia.split(" ");
String[] part4 = Sintok.split(" ");
for(int i=1;i<10; i++){
try {
Thread.sleep(130);
} catch (InterruptedException ex) {
Logger.getLogger(thread1.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println(part1[i]+"");
}
for(int i=1;i<5; i++){
try {
Thread.sleep(170);
} catch (InterruptedException ex) {
Logger.getLogger(thread1.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println(part2[i]+"");
}
for(int i=1;i<8; i++){
try {
Thread.sleep(200);
} catch (InterruptedException ex) {
Logger.getLogger(thread2.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println(part3[i]+"");
}
for(int i=1;i<6; i++){
try {
Thread.sleep(220);
} catch (InterruptedException ex) {
Logger.getLogger(thread2.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println(part4[i]+"");
}
}
}
package worddisplay;
import java.util.ArrayList;
/**
*
* @author master lab
*/
class Up extends Thread {
public void run(){
ArrayList<String> arraylist = new ArrayList<String>();
arraylist.add("Universiti");
arraylist.add("Utara");
arraylist.add("Malaysia");
arraylist.add("Sintok");
arraylist.add("Kedah");
arraylist.add("Malaysia");
try{ for(String str: arraylist){
System.out.println(str);
}
Thread.sleep(2000);
}catch(Exception e){
e.printStackTrace();
}
}
}
class Down extends Thread {
public void run(){
String s = "Universiti Utara Malaysia Sintok Kedah Malaysia";
try{ for(int i=0; i<s.length();i++){
char c = s.charAt(i);
System.out.println(c);
}
Thread.sleep(5000);
} catch (Exception e){
e.printStackTrace();
}
}
}
public class WordDisplay{
public static void main(String[] args) {
Up t1 = new Up();
Down t2 = new Down();
t1.start();
t2.start();
}
}
public class test2 extends Thread{
public static void main (String args [])
{
word1 w1 = new word1();
word2 w2 = new word2();
w1.start();
w2.start();
}
}
class word1 extends Thread { public void run() { try { System.out.println("universiti utara malaysia sintok kedah malaysia"); sleep(1000); } catch (Exception e) { e.printStackTrace(); } } }
class word2 extends Thread { public void run(){ try { String s1 = "universiti"; char[] c = s1.toCharArray(); System.out.println("universiti"); for(int i=1;i<s1.length();i++) { System.out.println(c[i]); sleep(500); }
System.out.println();
System.out.println("utara");
String s2 = "utara";
char[] c1 = s2.toCharArray();
for (int j=1;j<s2.length();j++){
System.out.println(c1[j]);
sleep(500);
}
System.out.println();
System.out.println("malaysia");
String s3 = "malaysia";
char[] c2 = s3.toCharArray();
for (int k=1;k<s3.length();k++){
System.out.println(c2[k]);
sleep(500);
}
System.out.println();
System.out.println("sintok");
String s4 = "sintok";
char[] c3 = s4.toCharArray();
for (int l=1;l<s4.length();l++){
System.out.println(c3[l]);
sleep(500);
}
System.out.println();
System.out.println("kedah");
String s5 = "kedah";
char[] c4 = s5.toCharArray();
for (int m=1;m<s5.length();m++){
System.out.println(c4[m]);
sleep(500);
}
System.out.println();
System.out.println("malaysia");
String s6 = "malaysia";
char[] c5 = s6.toCharArray();
for (int n=1;n<s6.length();n++){
System.out.println(c5[n]);
sleep(500);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
class thread1 extends Thread{
@Override
public void run(){
String s1 = "universiti utara malaysia sintok kedah malaysia";
System.out.println(s1);
}}
class thread2 extends Thread{
@Override
public void run(){
char ch;
String s2 = "universiti utara malaysia sintok kedah malaysia";
String[] word = s2.split("\\s");
for (int k = 0; k < word.length; k++) {
System.out.println(word[k]);
for (int i = 1; i < word[k].length(); i++) {
ch = word[k].charAt(i);
System.out.println(ch);
}}}}
class Main {
public static void main(String[] args) {
thread1 t1=new thread1();
thread2 t2=new thread2();
t1.start();
t2.start();
}
}
public class Main extends Thread {
public static void main(String [] args){
Thread1 T1 = new Thread1();
T1.start();
}//main
}//class
//credits to https://www.w3schools.com/java/java_for_loop.asp
//credits to ....http://zetcode.com/lang/java/strings/
import java.lang.*;
public class Thread1 extends Thread {
public void run() {
try {
String s = "universiti utara malaysia sintok kedah malaysia";
String[] words = s.split(" ");
for (String word : words) {
System.out.println(word);
sleep(5000);
//Thread.yield();
for (int i = 1; i <= word.length()-1; i++) {
System.out.println(word.charAt(i));
sleep(1000);
//Thread.yield();
}//for
}
} catch (InterruptedException e) {
e.printStackTrace();
}//catch
}
}
package threadstring; class threadstring { public static void main(String[] args){ System.out.println("Universiti Utara Malaysia Sintok Kedah Malaysia"); String str="Universiti Utara Malaysia Sintok Kedah Malaysia"; thread1 t1 = new thread1(str); thread2 t2 =new thread2(str); t2.start(); t1.start(); } }
package threadstring; class thread1 extends Thread{ String str; thread1(String str){ this.str=str; }
public void run() {
String[] arr = str.split("\\s");
for (int i = 0; i < arr.length; i++) {
if (arr[i] != null && !"".equals(arr[i])) {
int strLen = arr[i].length();
for (int j = 1; j < strLen; j++) {
System.out.println(arr[i].charAt(j));
}
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Thread.yield();
}
}
package threadstring; class thread2 extends Thread { boolean waiting = true; boolean ready = false; String str;
thread2(String str) {
this.str = str;
}
public void run() {
String[] arr = str.split("\\s");
for (String a : arr) {
System.out.println(a);
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Thread.yield();
}
}
public class ThreadTest extends Thread {
public void run(){
try{
System.out.println("\"universiti utara malaysia sintok kedah malaysia\"");
sleep(1000);
}catch (Exception e){
e.printStackTrace();
}
}
}
class Thread2 extends Thread{
public void run(){
try{
String s1 = "universiti";
char[] c = s1.toCharArray();
System.out.println(s1);
for (int i = 1; i < c.length;i++){
System.out.println(c[i]);
sleep(500);
}
System.out.println();
String s2 = "utara";
char [] c1 = s2.toCharArray();
System.out.println(s2);
for(int i = 1;i < c1.length;i++){
System.out.println(c1[i]);
sleep(500);
}
System.out.println();
String s3 = "malaysia";
char[] c2 = s3.toCharArray();
System.out.println(s3);
for(int i = 1;i < c2.length;i++){
System.out.println(c2[i]);
sleep(500);
}
String s4 = "sintok";
char[] c3 = s4.toCharArray();
System.out.println(s4);
for(int i = 1;i < c3.length;i++){
System.out.println(c3[i]);
sleep(500);
}
String s5 = "kedah";
char[] c4 = s5.toCharArray();
System.out.println(s5);
for(int i = 1;i < c4.length;i++){
System.out.println(c4[i]);
sleep(500);
}
String s6 = "malaysia";
char[] c5 = s6.toCharArray();
System.out.println(s6);
for(int i = 1;i < c5.length;i++){
System.out.println(c5[i]);
sleep(500);
}
}catch (Exception e ){
e.printStackTrace();
}
}
public static void main(String args[])
{
ThreadTest t1=new ThreadTest();
Thread2 t2=new Thread2();
t1.start();
t2.start();
}
}
![RB4O7E95Z2}E$SY}C} XS41](https://user-images.githubusercontent.com/55168412/66054340-68409a80-e566-11e9-994b-ffae86e4f39c.png)
![J_ MP6~XBE@GZ}}MJPCSE7](https://user-images.githubusercontent.com/55168412/66054349-6a0a5e00-e566-11e9-8d13-0bb6750c9c1f.png)
···
package threads;
/**
*
* @author Asus
*/
class String1 extends Thread {
public void run(){
String uni = new String ("Universiti");
String uta = new String (" Utara");
String myr = new String (" Malaysia");
String sin = new String (" Sintok");
String ked = new String (" Kedah");
String mal = new String (" Malaysia");
try{
System.out.println(uni + uta + myr + sin + ked + mal);
}catch (Exception e){
e.printStackTrace();
}
}
}
package threads;
/**
*
* @author Asus
*/
class String0 extends Thread {
public void run(){
try{
String uni = "Universiti";
char arr [] = uni.toCharArray();
System.out.println("Universiti");
for(int u=1;u<10;u++)
{
System.out.println(arr[u]);
sleep(200);
}
System.out.println("Utara");
String uta = "Utara";
char s1 [] = uta.toCharArray();
for(int t=1;t<5;t++)
{
System.out.println(s1[t]);
sleep(200);
}
System.out.println("Malaysia");
String myr = "Malaysia";
char s2 [] = myr.toCharArray();
for (int l=1;l<8; l++)
{
System.out.println(s2[l]);
sleep(200);
}
System.out.println("Sintok");
String sin = "Sintok";
char s4 [] = sin.toCharArray();
for(int d=1;d<6;d++)
{
System.out.println(s4[d]);
sleep(200);
}
System.out.println("Kedah");
String ked = "Kedah";
char s3 [] = ked.toCharArray();
for(int c=1;c<5;c++)
{
System.out.println(s3[c]);
sleep(200);
}
System.out.println("Malaysia");
String mur = "Malaysia";
char s5 [] = mur.toCharArray();
for (int j=1;j<8; j++)
{
System.out.println(s5[j]);
sleep(200);
}
}catch (Exception e)
{
e.printStackTrace();
}
}
}
package threads;
/**
*
* @author Asus
*/
public class Threads {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
String1 all = new String1 ();
String0 uni = new String0 ();
all.start();
uni.start();
}
}
public class Exercise {
private static Thread words = new words();
private static Thread character = new character();
public static void main(String[] args){
words.start();
character.start();
}
}
public class words extends Thread{
private static String Sentences = "Universiti Utara Malaysia Sintok Kedah Malaysia";
static String[] wordsS = Sentences.split(" ");
public void run() {
for (String s : wordsS) {
System.out.println(s);
yield();
}
}
}
public class character extends Thread {
public void run() {
for (int i = 0; i < words.wordsS.length; i++) {
for (int j = 1; j < words.wordsS[i].length(); j++) {
System.out.println(words.wordsS[i].charAt(j));
}
yield();
}
}
}
package com.company;
import java.io.*;
import java.util.*;
class Words {
public static void main(String[] args) {
String ww = "universiti utara malaysia sintok kedah malaysia";
Word w1 = new Word(ww);
WordC w2 = new WordC(ww);
w1.start();
w2.start();
}
}
class Word extends Thread {
String ww;
public Word(String ww) {
this.ww = ww;
}
public void run() {
try{
String[] arrOfStr = ww.split(" ");
for (String w : arrOfStr) {
System.out.println(w);
sleep(1000);
}} catch (Exception e) {
e.printStackTrace();
}
}
}
class WordC extends Thread {
String ww;
public WordC(String ww) {
this.ww = ww;
}
public void run() {
try {
String[] arrOfStr = ww.split(" ");
for (String w : arrOfStr) {
for(int i=1; i<w.length();i++){
char c [] = w.toCharArray();
System.out.println(c[i]);
}
sleep(1000);
}
}
catch (Exception e) {
System.out.println(e);
}
}
}
import java.util.Scanner;
class thread1 extends Thread {
String sentences;
public thread1(String sentenceS) {
this.sentences = sentenceS;
}
public void run(){
try {
String[] wordS = sentences.split(" ");
for (String wordString : wordS) {
System.out.println(wordString);
sleep(1000);
}} catch (Exception e) {
e.printStackTrace();
}
}
}
class thread2 extends Thread {
String sentences;
public thread2(String sentenceS) {
this.sentences = sentenceS;
}
public void run(){
try {
String[] wordS = sentences.split(" ");
for (String sentenceS : wordS) {
for(int i=1; i<sentenceS.length();i++){
char c [] = sentences.toCharArray();
System.out.println(c[i]);
}
sleep(1000);
}
}
catch (Exception e) {
System.out.println(e);
}
}
}
public class test123 {
public static void main(String[] args) {
String sentenceS ="universiti utara malaysia sintok kedah malaysia";
thread1 wordS = new thread1(sentenceS);
thread2 characterS = new thread2(sentenceS);
wordS.setPriority(Thread.MAX_PRIORITY);
characterS.setPriority(Thread.MIN_PRIORITY);
wordS.start();
characterS.start();
}
}
class thread1 extends Thread {
@Override
public void run() {
String word1 = "Universiti Utara Malaysia Sintok Kedah Malaysia";
String inEachLine = word1.replace(" ", System.lineSeparator());
try {
for (String word : word1.split(" ")) {
System.out.println(word);
Thread.yield();
sleep(1000);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
class thread2 extends Thread {
@Override
public void run() {
String word1 = "Universiti Utara Malaysia Sintok Kedah Malaysia";
String inEachLine = word1.replace(" ", System.lineSeparator());
try {
for (String word : word1.split(" ")) {
for (int i = 1; i < word.length(); i++) {
System.out.println(word.charAt(i));
}
Thread.yield();
sleep(1000);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class run {
public static void main(String args[]) {
Thread t1 = new thread1();
Thread t2 = new thread2();
t1.setPriority(Thread.MIN_PRIORITY); //Min Priority
t2.setPriority(Thread.MAX_PRIORITY); //Max Priority
t1.start();
t2.start();
}
}
public class main {
public static void main(String[] args) {
String s = "universiti utara malaysia sintok kedah malaysia";
threads t1 = new threads(s);
threads2 t2 = new threads2(s);
t1.start();
t2.start();
}
}
public class threads extends Thread {
String s;
public threads(String s) {
this.s = s;
}
public void run() {
try{
String[] arrOfStr = s.split(" ");
for (String w : arrOfStr) {
System.out.println(w);
sleep(1000);
}} catch (Exception e) {
e.printStackTrace();
}
}
}
public class threads2 extends Thread {
String s;
public threads2(String s) {
this.s = s;
}
public void run() {
try {
String[] arrOfStr = s.split(" ");
for (String w : arrOfStr) {
for(int i=1; i<w.length();i++){
char c [] = w.toCharArray();
System.out.println(c[i]);
}
sleep(1000);
}
}
catch (Exception e) {
System.out.println(e);
}
}
}
package issues4;
import java.util.logging.Level; import java.util.logging.Logger;
/*
@author Ahmed_Bawazir */ public class Issues4 {
/**
}
class Mythread1 extends Thread{
public void run(){
try {
System.out.println("universiti utara malaysia sintok kedah malaysia\n");
sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(Mythread1.class.getName()).log(Level.SEVERE, null, ex);
}
}
} class Mythread2 extends Thread{
public void run(){
String a ="Universiti";
char[]x =a.toCharArray();
System.out.println("Universiti");
for(int i=1;i<10;i++){
System.out.println(x[i]);
}
String a1 ="Utara";
char[]x1 =a1.toCharArray();
System.out.println("Utara");
for(int i=1;i<=4;i++){
System.out.println(x1[i]);
}
String a2 ="Malaysia";
char[]x2 =a2.toCharArray();
System.out.println("Malaysia");
for(int i=1;i<=7;i++){
System.out.println(x2[i]);
}
String a3 ="Sintok";
char[]x3 =a3.toCharArray();
System.out.println("Sintok");
for(int i=1;i<6;i++){
System.out.println(x3[i]);
}
String a4 ="Kedah";
char[]x4 =a4.toCharArray();
System.out.println("Kedah");
for(int i=1;i<5;i++){
System.out.println(x4[i]);
}
String a5 ="Malaysia";
char[]x5 =a5.toCharArray();
System.out.println("Malaysia");
for(int i=1;i<8;i++){
System.out.println(x5[i]);
}
}
}
public class DisplayWord extends Thread
{
public void run()
{
try
{
System.out.println("universiti utara malaysia sintok kedah malaysia");
sleep(1000);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
class Thread2 extends Thread
{
public void run()
{
try
{
String s1 = "universiti";
char[] c = s1.toCharArray();
System.out.println("universiti");
for(int i=1;i<=9;i++)
{
System.out.println(c[i]);
sleep(500);
}
System.out.println();
System.out.println("utara");
String s2 = "utara";
char[] c1 = s2.toCharArray();
for (int j=1;j<=4;j++){
System.out.println(c1[j]);
sleep(500);
}
System.out.println();
System.out.println("malaysia");
String s3 = "malaysia";
char[] c2 = s3.toCharArray();
for (int k=1;k<=7;k++){
System.out.println(c2[k]);
sleep(500);
}
System.out.println();
System.out.println("sintok");
String s4 = "sintok";
char[] c3 = s4.toCharArray();
for (int l=1;l<=5;l++){
System.out.println(c3[l]);
sleep(500);
}
System.out.println();
System.out.println("kedah");
String s5 = "kedah";
char[] c4 = s5.toCharArray();
for (int m=1;m<=4;m++){
System.out.println(c4[m]);
sleep(500);
}
System.out.println();
System.out.println("malaysia");
String s6 = "malaysia";
char[] c5 = s6.toCharArray();
for (int n=1;n<=7;n++){
System.out.println(c5[n]);
sleep(500);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
class Issue3
{
public static void main(String args[])
{
DisplayWord t1=new DisplayWord();
Thread2 t2=new Thread2();
t1.start();
t2.start();
}
}
output: https://gist.github.com/FatihahFauzi/cae549faa4f2aa87d4b3eec1c9aa22ed
import java.util.Arrays;
import java.util.Scanner;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.IntStream;
public class one {
public static void main(String[] args) {
String input = new Scanner(System.in).nextLine();
String[] splitInput = input.split(" ");
AtomicInteger counter = new AtomicInteger();
new Thread(() -> Arrays.stream(splitInput)
.forEach(word -> {
while (counter.get() % 2 != 0) ;
System.out.println(word);
counter.incrementAndGet();
})).start();
new Thread(() -> Arrays.stream(splitInput)
.forEach(word -> {
while (counter.get() % 2 != 1) ;
IntStream.range(1, word.length()).forEach(i -> System.out.println(word.charAt(i)));
counter.incrementAndGet();
}))
.start();
}
}
Instruction
Write a Java program using TWO (2) threads to read a string of words. The first thread will display every word in the string and the second thread will display each of the characters for the displayed word.
Example of string
"universiti utara malaysia sintok kedah malaysia"
Example of output:
universiti n i v e r s i t i utara t a r a malaysia a l a y s i a sintok . . . . .(continue until finish)
Submission