By GokiSoft.com| 15:29 26/07/2023|
Java Advanced

Sử dụng MultiThreading sinh ký tự a-zA-Z trong java

Viết chương trình thực hiện yêu cầu sau

- Thread 1 thực hiện sinh ngẫu nhiên các ký tự a-z, cứ 2s thì sinh 1 lần

- Thread 2 thực hiện biến các ký tự thường được sinh ra từ Thread 1 thành ký tự hoa, cứ sau 1s thì thực hiện 1 lần.

Yêu cầu đồng bộ 2 Thread (wait, notify)

- Nếu tổng thời gian xử lý của 2 Thread là 20s thì dựng 2 thread lại.

Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)

Do Trung Duc [T2008A]
Do Trung Duc

2021-03-18 08:25:44



/*
 * 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 Thread_InchuthuongthanhchuHoa;

/**
 *
 * @author TrungDuc
 */
public class Data {
    char character;
    int count =0;

    public char getCharacter() {
        return character;
    }

    public void setCharacter(char character) {
        this.character = character;
    }
    
}



Do Trung Duc [T2008A]
Do Trung Duc

2021-03-18 08:25:28



/*
 * 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 Thread_InchuthuongthanhchuHoa;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author TrungDuc
 */
public class Thread2 extends Thread {

    Data data;

    public Thread2(Data data) {
        this.data = data;
    }

    @Override
    public void run() {
        synchronized (data) {
            while (true) {
                data.notifyAll();
                try {
                    data.wait();
                } catch (InterruptedException ex) {
                    Logger.getLogger(Thread2.class.getName()).log(Level.SEVERE, null, ex);
                }

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ex) {
                    Logger.getLogger(Thread2.class.getName()).log(Level.SEVERE, null, ex);
                }
                char character = data.getCharacter();
                char characterCase = (char) (character - 32);
                System.out.println("T2 ky tu in hoa:" + characterCase);
                data.count += 1;
                if (data.count > 20) {
                    System.exit(0);
                }
            }
        }
    }

}



Do Trung Duc [T2008A]
Do Trung Duc

2021-03-18 08:25:09



/*
 * 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 Thread_InchuthuongthanhchuHoa;

import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author TrungDuc
 */
public class Thread1 extends Thread {

    Data data;

    public Thread1(Data data) {
        this.data = data;
    }

    @Override
    public void run() {
        try {
            Thread.sleep(100);
        } catch (InterruptedException ex) {
            Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
        }
        Random random = new Random();

        synchronized (data) {
            while (true) {
                try {
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
                    }

                    int rad = random.nextInt(26);
                    data.setCharacter((char) (rad + 97));
                    System.out.println("T1 sinh ra ky tu: " + data.getCharacter());
                    data.count += 2;
                    if (data.count > 20) {
                        System.exit(0);
                    }

                    data.notifyAll();
                    data.wait();
                } catch (InterruptedException ex) {
                    Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

    }
}



Do Trung Duc [T2008A]
Do Trung Duc

2021-03-18 08:24:55



/*
 * 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 Thread_InchuthuongthanhchuHoa;

/**
 *
 * @author TrungDuc
 */
public class Main {

    public static void main(String[] agrs) {
        Data data = new Data();
        Thread1 T1 = new Thread1(data);
        Thread2 T2 = new Thread2(data);
        T1.start();
        T2.start();
        

    }
}



Nguyễn Tiến Đạt [T2008A]
Nguyễn Tiến Đạt

2021-03-17 14:03:11


#Data.java


/*
 * 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 Java2.lesson4.MultiThreadazAZ;

/**
 *
 * @author MyPC
 */
public class Data {
    char randomChar;
    int count = 0;

    public char getRandomChar() {
        return randomChar;
    }

    public void setRandomChar(char randomChar) {
        this.randomChar = randomChar;
    }
    
}


#Main.java


/*
 * 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 Java2.lesson4.MultiThreadazAZ;

/**
 *
 * @author MyPC
 */
public class Main {
    public static void main(String[] args) {
        Data data = new Data();
        Thread1 t1 = new Thread1(data);
        Thread2 t2 = new Thread2(data);
        t1.start();
        t2.start();
    }
}


#Thread1.java


/*
 * 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 Java2.lesson4.MultiThreadazAZ;

import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author MyPC
 */
public class Thread1 extends Thread{
    Data data;

    public Thread1(Data data) {
        this.data = data;
    }
    
    @Override
    public void run() {
        try {
            sleep(100);
        } catch (InterruptedException ex) {
            Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
        }
        Random random = new Random();
        while (true) {            
            synchronized(data){
                try {
                    sleep(2000);
                    data.count+=2;
                } catch (InterruptedException ex) {
                    Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
                }
                
                int rand = random.nextInt(26);
                data.setRandomChar((char)(rand+97));
                System.out.println("Char random: " + (char)(rand+97));
                
                data.notifyAll();
                
                try {
                    data.wait();
                    
                } catch (InterruptedException ex) {
                    Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
                }
                if(data.count >= 20){
                    System.exit(0);
                }
            }
        }
    }
    
}


#Thread2.java


/*
 * 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 Java2.lesson4.MultiThreadazAZ;

import static java.lang.Thread.sleep;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author MyPC
 */
public class Thread2 extends Thread{
    Data data;

    public Thread2(Data data) {
        this.data = data;
    }
    
    @Override
    public void run() {
        while (true) {            
            synchronized(data){
                try {
                    data.notifyAll();
                    data.wait();
                } catch (InterruptedException ex) {
                    Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
                }
                try {
                    sleep(1000);
                    data.count++;
                } catch (InterruptedException ex) {
                    Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
                }
                char a = (char) (data.getRandomChar()-32);
                data.setRandomChar(a);
                System.out.println("Char Spec: " + a);
                if(data.count >= 20) System.exit(0);
            }
        }
    }
}



vuong huu phu [T2008A]
vuong huu phu

2021-03-17 13:30:28



/*
 * 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 javaapplication34;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Admin
 */
public class Thread2 extends Thread{
    thread_cs cs;

    public Thread2(thread_cs cs) {
        this.cs = cs;
    }

    @Override
    public void run() {
      final long Bat_dau = System.currentTimeMillis();
        for (int i = 0; i < 20; i++) {
            
            synchronized(cs){
                try {
                    cs.wait();
                } catch (InterruptedException ex) {
                    Logger.getLogger(Thread2.class.getName()).log(Level.SEVERE, null, ex);
                }
             int c =  cs.getC()-32;
             char cs1 = (char)(c);
             System.out.println("T2 = "+cs1);
               cs.notifyAll();
               final long end = System.currentTimeMillis();
               if ((end - Bat_dau)>=20000) {
                   stop();
                }    
                try {
                    sleep(2000);
                } catch (InterruptedException ex) {
                    Logger.getLogger(Thread2.class.getName()).log(Level.SEVERE, null, ex);
                }
                
        }
         
        }
        
    }
}



vuong huu phu [T2008A]
vuong huu phu

2021-03-17 13:29:21



/*
 * 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 javaapplication34;

import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Admin
 */
public class Thread1 extends Thread{
     thread_cs cs;

    public Thread1(thread_cs cs) {
        this.cs = cs;
    }
    @Override
    public void run() {
         try {
             sleep(100);
         } catch (InterruptedException ex) {
             Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
         }
          final long Bat_dau = System.currentTimeMillis();
        Random rd = new Random();
        for (int i = 0; i < 20; i++) {
            synchronized(cs){
                
            int c = rd.nextInt(26)+97;
            char c1 = (char)(c);
            cs.setC(c);
            System.out.println("T1 = "+c1); 
            cs.notifyAll();
            final long end = System.currentTimeMillis();
            if ((end - Bat_dau)>=20000) {
                  stop();
                 }  
             try {
                    cs.wait();
                } catch (InterruptedException ex) {
                    Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
                }
            try {
                sleep(2000);
            } catch (InterruptedException ex) {
                Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
            }
                
            }
    
}}}



vuong huu phu [T2008A]
vuong huu phu

2021-03-17 13:29:03



/*
 * 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 javaapplication34;

/**
 *
 * @author Admin
 */
public class thread_cs {
       int c ;

    public int getC() {
        return c;
    }

    public void setC(int c) {
        this.c = c;
    }
    
}



vuong huu phu [T2008A]
vuong huu phu

2021-03-17 13:28:40



/*
 * 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 javaapplication34;

/**
 *
 * @author Admin
 */
public class Test {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
          thread_cs cs = new thread_cs();
       Thread1 t1 = new Thread1(cs);
       t1.start();
       Thread2 t2 = new Thread2(cs);
       t2.start();
    }
    
}



hoangkhiem [C1907L]
hoangkhiem

2020-05-24 22:43:09



/*
 * 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 Java2MultiThread;

/**
 *
 * @author Admin
 */
public class Ex1 {

    public static void main(String[] args) {
        SharedData sharedData = new SharedData();
        Thread1 L1 = new Thread1(sharedData);
        Thread2 L2 = new Thread2(sharedData);
        L1.start();
        L2.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 Java2MultiThread;

import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Admin
 */
public class Thread1 extends Thread {

    SharedData sharedData;

    public Thread1(SharedData sharedData) {
        this.sharedData = sharedData;
    }


    @Override
    public void run() {
        Random rd = new Random();
        sharedData.setBegin(System.currentTimeMillis());
        while(true){
        synchronized (sharedData) {
            char kt = (char) ( rd.nextInt(26)+ 97 );
            sharedData.setNn(kt);
            System.out.println(sharedData.getNn());
            try {
                if ((System.currentTimeMillis() - sharedData.begin) > 20000) {
                    break;
                }
                sleep(2000);
            } catch (InterruptedException ex) {
                Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                sharedData.notifyAll();
                sharedData.wait();
            } catch (InterruptedException ex) {
                Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        }
    }
    

}



/*
 * 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 Java2MultiThread;

import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Admin
 */
public class Thread2 extends Thread {

    SharedData sharedData;

    public Thread2(SharedData sharedData) {
        this.sharedData = sharedData;
    }

    @Override
    public void run() {
        Random rd = new Random();
        while (true) {
            synchronized (sharedData) {
                
                System.out.println(">>" + Character.toUpperCase(this.sharedData.getNn()));
                try {
                    if ((System.currentTimeMillis() - sharedData.begin) > 20000) {
                        break;
                    }
                    sleep(1000);
                } catch (InterruptedException ex) {
                    Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
                }
                try {
                    sharedData.notifyAll();
                    sharedData.wait();
                } catch (InterruptedException ex) {
                    Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

        }
        synchronized (sharedData) {
            sharedData.notifyAll();
        }
    }

  
}



/*
 * 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 Java2MultiThread;

/**
 *
 * @author Admin
 */
public class SharedData {

    public char nn;

    public long begin;

    public char getNn() {
        return nn;
    }

    public void setNn(char nn) {
        this.nn = nn;
    }

    public long getBegin() {
        return begin;
    }

    public void setBegin(long begin) {
        this.begin = begin;
    }

 
}