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
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)
Hieu Ngo
2021-09-11 04:03:05
#Main.java
public class Main {
public static void main(String [] args) {
ThreadOne t1 = new ThreadOne();
ThreadTwo t2 = new ThreadTwo();
t1.start();
try {
t1.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
t2.start();
try {
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
t1.show();
System.out.println();
t2.show();
}
}
#ThreadOne.java
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Random;
public class ThreadOne 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() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Random random = new Random();
for(int i = 0 ;i<10;i++) {
int rad = random.nextInt(100);
list1.add(rad);
}
}
public void show() {
for(Integer integer : list1) {
System.out.print(integer+ " ");
};
}
}
#ThreadTwo.java
import java.util.ArrayList;
import java.util.Random;
public class ThreadTwo 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() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Random random = new Random();
for(int i =0; i< 10; i++) {
char characterRad = (char)('a' + random.nextInt(26));
list2.add(characterRad);
}
}
public void show() {
for (Character c : list2) {
System.out.print(c+ " ");
}
}
}
hieuvm0512
2021-08-20 17:53: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 com.mycompany.thread;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author vuive
*/
public class Main {
public static void main(String[] args){
Thread1 t1 = new Thread1();
Thread2 t2 = new Thread2();
System.out.println("Doi 2s");
t1.sleep();
t1.start();
try {
t1.join();
} catch (InterruptedException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("Thread1 xong, doi 1s");
t2.sleep();
t2.start();
System.out.println("Thread 1");
t1.show();
System.out.println("Thread 2");
t2.show();
}
}
hieuvm0512
2021-08-20 17:53: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 com.mycompany.thread;
import java.util.ArrayList;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author vuive
*/
public class Thread2 extends Thread{
ArrayList<Character> list2 = new ArrayList<>();
@Override
public void run(){
Random r = new Random();
char a;
for(int i=0;i<10;i++){
a = (char)(r.nextInt(26)+'a');
list2.add(a);
}
}
public void show(){
for(Character c : list2){
System.out.print(" "+c);
}
}
public void sleep(){
try {
Thread2.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
hieuvm0512
2021-08-20 17:52:49
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.mycompany.thread;
import java.util.ArrayList;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author vuive
*/
public class Thread1 extends Thread{
ArrayList<Integer> list1 = new ArrayList<>();
@Override
public void run(){
Random random = new Random();
int r;
for(int i=0;i<10;i++){
r = random.nextInt(100)+1;
list1.add(r);
}
}
public void show(){
for(Integer n : list1){
System.out.print(n+" ");
}
}
public void sleep(){
try {
Thread1.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Đào Mạnh Dũng
2021-08-20 11:28:54
#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 pkg1097;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Đào Dũng
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Thread1 t1 = new Thread1();
t1.start();
Thread2 t2 = new Thread2();
t2.start();
try {
TimeUnit.SECONDS.sleep(10);
t1.stop();
} catch (InterruptedException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println(t1.list1.toString());
try {
TimeUnit.SECONDS.sleep(10);
t2.stop();
} catch (InterruptedException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println(t2.list1.toString());
}
}
#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 pkg1097;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Đào Dũng
*/
public class Thread1 extends Thread{
public ArrayList<Integer> list1;
public Thread1() {
this.list1 = new ArrayList<>();
}
@Override
public void run() {
for (;;) {
list1.add((int)(101*Math.random()));
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
#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 pkg1097;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Đào Dũng
*/
public class Thread2 extends Thread{
public ArrayList<Character> list1;
public Thread2() {
this.list1 = new ArrayList<>();
}
@Override
public void run() {
for (;;) {
list1.add((char)(26*Math.random()+97));
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {
Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
Nguyễn Anh Vũ
2021-03-24 07:03:56
/*
* 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 bt4;
import java.util.ArrayList;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Nav
*/
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);
}
}
}
}
Nguyễn Anh Vũ
2021-03-24 07:03:24
/*
* 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 bt4;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Nav
*/
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 Anh Vũ
2021-03-24 07:01: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 bt4 ;
import java.util.ArrayList;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Nav
*/
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);
}
}
}
}
Triệu Văn Lăng
2021-03-19 08:14: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 bai1097;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author MTLS
*/
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("Ket Qua");
for (int i = 0; i < t1.list1.size(); i++) {
System.out.println("Thread 1: " + t1.list1.get(i) );
}
for (int i = 0; i < t2.list2.size(); i++) {
System.out.println("Thread 2: " + t2.list2.get(i));
}
}
}
Triệu Văn Lăng
2021-03-19 08:14:22
/*
* 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 Thread2 extends Thread{
ArrayList<Character> list2 = new ArrayList<>();
@Override
public void run() {
Random rd = new Random();
for (int i = 0; i < 10; i++) {
try {
char c = (char) (rd.nextInt(26) + 97);
list2.add(c);
Thread2.sleep(2000);
} catch (InterruptedException ex) {
Logger.getLogger(Thread2.class.getName()).log(Level.SEVERE, null, ex);
}
}
System.out.println("Done Thread2");
}
}