By GokiSoft.com| 15:42 17/07/2023|
Java Advanced

Thread thực hiện in ngẫu nhiên các số nguyên và ký tự

Tạo lớp Thread1 kế thừa Thread gồm các thuộc tính ArrayList<Integer> list1

- Thread này thực hiện sau 1s sinh ngẫu nhiên các số tự nhiêu chạy từ 0-100 -> lưu kết quả vào mảng list1

Tạo lớp Thread2 kế thừa Thread gồm các thuộc tính ArrayList<Char> list2

- Thread này thực hiện sau 2s thi sinh ngẫu nhiên các ký tự từ a-z và lưu vào mảng list2

Trong phương thức main của lớp Test tạo ra 2 thread t1 và t2 lần lượt từ Thread1 và Thread2

Khi t1 đã sinh 10 lần -> thực hiện dừng t1

Khi t2 đã sinh đc 10 ký tự thì stop t2

Sau khi 2 thread 1 và 2 kết thúc. Thực hiện in kết quả của các thread trên thread main

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

5

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

Triệu Văn Lăng [T2008A]
Triệu Văn Lăng

2021-03-19 08:14:08



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

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

/**
 *
 * @author MTLS
 */
public class Thread1 extends Thread{
    ArrayList<Integer> list1 = new ArrayList<>();

    @Override
    public void run() {
        Random rd = new Random();
        for (int i = 0; i < 10; i++) {
            int n = rd.nextInt(100);
            list1.add(n);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
                Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        System.out.println("Done Thread1");
    }
    
}



hainguyen [T2008A]
hainguyen

2021-03-17 09:59:13



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

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

/**
 *
 * @author Admin
 */
public class Thread2 extends Thread{
    ArrayList<Character> list2 = new ArrayList<>();

    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            Random random = new Random();
            list2.add((char) (random.nextInt(26) + 97));
            
            try {
                sleep(2000);
            } catch (InterruptedException ex) {
                 Logger.getLogger(Thread2.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    
    
}



hainguyen [T2008A]
hainguyen

2021-03-17 09:58:59



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

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

/**
 *
 * @author Admin
 */
public class Thread1 extends Thread{
    ArrayList<Integer> list1 = new ArrayList<>();

    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            Random random = new Random();
            list1.add(random.nextInt(100));
            
            try {
                sleep(1000);
            } catch (InterruptedException ex) {
                Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}



hainguyen [T2008A]
hainguyen

2021-03-17 09:58:46



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

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

/**
 *
 * @author Admin
 */
public class Main {
    public static void main(String[] args) {
        Thread1 t1 = new Thread1();
        Thread2 t2 = new Thread2();
        
        t1.start();
        t2.start();
        
        try {
            t1.join();
        } catch (InterruptedException ex) {
             Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        try {
            t2.join();
        } catch (InterruptedException ex) {
             Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("List 1: " + t1.list1);
        System.out.println("List 2: " + t2.list2);
    }
}



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

2021-03-15 13:42:52


#Test.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.lesson3.Thread;

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

/**
 *
 * @author MyPC
 */
public class Test {
    public static void main(String[] args) {
        Thread1 t1 = new Thread1();
        Thread2 t2 = new Thread2();
        t1.start(); // vao ham run
        t2.start(); // vao ham run
        
        //tuong gio chan luong t1 lai -> thuc hien het luong t1 thi moi chay den code tiep theo
        try {
            t1.join();
        } catch (InterruptedException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        }
        //tuong gio chan luong t2 lai -> thuc hien het luong t2 thi moi chay den code tiep theo
        try {
            t2.join();
        } catch (InterruptedException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("List 1 = " + t1.list1);
        System.out.println("List 2 = " + t2.list2);
    }
}


#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.lesson3.Thread;

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


/**
 *
 * @author MyPC
 */
public class Thread1 extends Thread{
    ArrayList<Integer> list1 = new ArrayList<>();

    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            Random random = new Random();
            list1.add(random.nextInt(101));
            //delay chay thread 1 giay
            try {
                sleep(1000);
            } catch (InterruptedException ex) {
                Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
            }
            System.out.println("add to t1");
        }

    }
    
}


#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.lesson3.Thread;

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

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

    ArrayList<Character> list2 = new ArrayList<>();

    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            Random random = new Random();
            list2.add((char) (random.nextInt(26) + 97));
            //delay chay thread 2 giay
            try {
                sleep(2000);
            } catch (InterruptedException ex) {
                Logger.getLogger(Thread2.class.getName()).log(Level.SEVERE, null, ex);
            }
            
         System.out.println("added to t2");
        }
    }

}



vuong huu phu [T2008A]
vuong huu phu

2021-03-15 13:27:39



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

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

/**
 *
 * @author Admin
 */
public class Thread2 extends Thread{
    ArrayList<Character> list2 = new ArrayList();

    @Override
    public void run() {
        Random random=new Random();
        for (int i = 1; i < 10; i++) {
            char c = (char) (random.nextInt(26)+97);
            list2.add(c);
        try {
            Thread.sleep(2000);
        } catch (InterruptedException ex) {
            Logger.getLogger(Thread2.class.getName()).log(Level.SEVERE, null, ex);
        }
    }System.out.println("Chay xong thread 2 ");
     }
}



vuong huu phu [T2008A]
vuong huu phu

2021-03-15 13:26:58



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

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

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

    ArrayList<Integer> list1 = new ArrayList<>();

    @Override
    public void run() {
        Random random = new Random();
        for (int i = 1; i < 10; i++) {
            int n = random.nextInt(100);
            list1.add(n);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
                Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        System.out.println("Chay xong thread 1 ");
    }

}



vuong huu phu [T2008A]
vuong huu phu

2021-03-15 13:26:41



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

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

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

    public static void main(String[] args) {

        Thread1 th1 = new Thread1();
        Thread2 th2 = new Thread2();
        th1.start();
        th2.start();
        try {
            th1.join();
            th2.join();
        } catch (InterruptedException ex) {
            Logger.getLogger(JavaApplication31.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println();
        System.out.println("========== Thread 1 ==========");
        for (int i = 0; i < th1.list1.size(); i++) {
            System.out.println("T1 = " + th1.list1.get(i) + ", ");
        }

        System.out.println("========== Thread 2 ==========");
        for (int i = 0; i < th2.list2.size(); i++) {
            System.out.println("T2 = " + th2.list2.get(i) + ", ");
        }

    }

}



hoangkhiem [C1907L]
hoangkhiem

2020-05-24 23:19:37



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

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

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

    public static void main(String[] args) {
        Thread1 t1 = new Thread1();
        Thread2 t2 = new Thread2();
        t1.start();
        try {
            t1.join();
        } catch (InterruptedException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        }
        t2.start();
        try {
            t2.join();
        } catch (InterruptedException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("List Int");
        t1.list1.forEach((integer) -> {
            System.out.println(integer);
        });
        
        System.out.println("List Char");
        t2.list2.forEach((character) -> {
            System.out.println(character);
        });

    }
}



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

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

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

    ArrayList<Integer> list1 = new ArrayList();

    public ArrayList<Integer> getList1() {
        return list1;
    }

    public void setList1(ArrayList<Integer> list1) {
        this.list1 = list1;
    }
    @Override
    public void run(){
    Random rd = new Random();
        for (int i = 0; i < 10; i++) {
            int s = rd.nextInt(101);
            list1.add(s);
            System.out.println(s);
        try {
            sleep(1000);
        } 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 Inngaynhiencacsonguyentovakytu;

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

/**
 *
 * @author Admin
 */
public class Thread2 extends Thread{
    ArrayList<Character> list2 = new ArrayList();

    public ArrayList<Character> getList2() {
        return list2;
    }

    public void setList2(ArrayList<Character> list2) {
        this.list2 = list2;
    }

  
        @Override
    public void run(){
    Random rd = new Random();
        for (int i = 0; i < 10; i++) {
            char s = (char) (rd.nextInt(26)+97);
            list2.add(s);
            System.out.println(s);
        try {
            sleep(2000);
        } catch (InterruptedException ex) {
            Logger.getLogger(Thread2.class.getName()).log(Level.SEVERE, null, ex);
        }
        }
               
    }

}



trung [C1907L]
trung

2020-05-24 14:34:34



/*
 * 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 daerht1.newpackage;

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

/**
 *
 * @author prdox
 */
public class charAndNumberGenerator {
    public static void main(String[] args) {
        Thread1 t1 = new Thread1();
        Thread2 t2 = new Thread2();
        t1.start();
        t2.start();
        try {
            t1.join();
        } catch (InterruptedException ex) {
            Logger.getLogger(charAndNumberGenerator.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            t2.join();
        } catch (InterruptedException ex) {
            Logger.getLogger(charAndNumberGenerator.class.getName()).log(Level.SEVERE, null, ex);
        }
        for (int num : t1.list1) {
            System.out.println("t1 >> " + num);
        }
        for (char c : t2.list2) {
            System.out.println("t2 >> " + c);
        }
    }
}



/*
 * 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 daerht1.newpackage;

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

/**
 *
 * @author prdox
 */
public class Thread1 extends Thread{
    List<Integer> list1 = new ArrayList<>();

    @Override
    public void run() {
        for (int i = 0 ; i < 10; i++) {
            Random rng = new Random();
            list1.add(rng.nextInt(101));
            System.out.println("added to t1");
            try {
                sleep(1000);
            } 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 daerht1.newpackage;

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

/**
 *
 * @author prdox
 */
public class Thread2 extends Thread{
    List<Character> list2 = new ArrayList<>();

    @Override
    public void run() {
        for (int i = 0 ; i < 10; i++) {
            Random rng = new Random();
            list2.add((char) (rng.nextInt(26)+97));
            System.out.println("added to t2");
            try {
                sleep(2000);
            } catch (InterruptedException ex) {
                Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}