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)
Nguyễn Hoàng Anh
2020-05-24 02:43:26
/*
* 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 random_numberAndChar;
import java.util.ArrayList;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Redmibook 14
*/
public class Thread1 extends Thread {
SharedData sharedData;
public Thread1(SharedData sharedData) {
this.sharedData = sharedData;
}
@Override
public void run() {
Random rd = new Random();
synchronized (sharedData) {
for (int i = 0; i < 10; i++) {
int n = rd.nextInt(100);
sharedData.getList().add(n);
System.out.println(n);
try {
sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
}
sharedData.notifyAll();
try {
sharedData.wait();
} catch (InterruptedException ex) {
Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
}
synchronized (sharedData) {
sharedData.notifyAll();
}
}
int size = sharedData.getList().size();
System.out.println("T1");
for (int i = 0; i < size; i++) {
System.out.println(sharedData.getList().get(i));
}
}
}
}
/*
* 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 random_numberAndChar;
import java.util.ArrayList;
/**
*
* @author Redmibook 14
*/
public class SharedData {
ArrayList<Integer> list = new ArrayList();
ArrayList<Character> list2 = new ArrayList();
public ArrayList<Character> getList2() {
return list2;
}
public ArrayList<Integer> getList() {
return list;
}
}
/*
* 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 random_numberAndChar;
import static java.lang.Thread.sleep;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Redmibook 14
*/
public class Thread2 extends Thread {
SharedData sharedData;
public Thread2(SharedData sharedData) {
this.sharedData = sharedData;
}
@Override
public void run() {
Random rd = new Random();
synchronized (sharedData) {
for (int i = 0; i < 10; i++) {
char n = (char) (rd.nextInt(27) + 97);
sharedData.getList2().add(n);
System.out.println(n);
try {
sleep(2000);
} catch (InterruptedException ex) {
Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
}
sharedData.notifyAll();
try {
sharedData.wait();
} catch (InterruptedException ex) {
Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
}
// NOTE!!!!!!!!!!!!!!!
synchronized (sharedData) {
sharedData.notifyAll();
}
}
int size = sharedData.getList2().size();
System.out.println("T2");
for (int i = 0; i < size; i++) {
System.out.println(sharedData.getList2().get(i));
}
}
}
}
/*
* 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 random_numberAndChar;
/**
*
* @author Redmibook 14
*/
public class test {
public static void main(String[] args) {
SharedData sharedData = new SharedData();
Thread1 t1 = new Thread1(sharedData);
Thread2 t2 = new Thread2(sharedData);
t1.start();
t2.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 random_numberAndChar;
import java.util.ArrayList;
/**
*
* @author Redmibook 14
*/
public class SharedData {
ArrayList<Integer> list = new ArrayList();
ArrayList<Character> list2 = new ArrayList();
public ArrayList<Character> getList2() {
return list2;
}
public ArrayList<Integer> getList() {
return list;
}
}
Ngô Quang Huy
2020-05-23 11:43:18
/*
* 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 May22Thread2;
/**
*
* @author Administrator
*/
public class Main {
public static void main(String[] args) {
Thread1 t1=new Thread1();
t1.start();
Thread2 t2=new Thread2();
t2.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 May22Thread2;
import java.util.ArrayList;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Administrator
*/
public class Thread1 extends Thread{
ArrayList<Integer> list1 = new ArrayList<>();
public Thread1() {
}
public ArrayList<Integer> getList1() {
return list1;
}
public void setList1(ArrayList<Integer> list1) {
this.list1 = list1;
}
@Override
public void run() {
Random random = new Random();
for (int i = 0; i < 10; i++) {
list1.add(random.nextInt(10)+1);
System.out.println("1ADD");
try {
sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
}
}
System.out.print("List1: ");
for (Integer integer : list1) {
System.out.print("-->"+integer);
}
System.out.println(" ");
}
}
/*
* 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 May22Thread2;
import static java.lang.Thread.sleep;
import java.util.ArrayList;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Administrator
*/
public class Thread2 extends Thread{
ArrayList<Character> list2 = new ArrayList<>();
public Thread2() {
}
public ArrayList<Character> getList2() {
return list2;
}
public void setList2(ArrayList<Character> list2) {
this.list2 = list2;
}
@Override
public void run() {
Random random = new Random();
for (int i = 0; i < 10; i++) {
list2.add( (char) (random.nextInt(26)+97));
System.out.println("2ADD");
try {
sleep(2000);
} catch (InterruptedException ex) {
Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
}
}
System.out.print("List2: ");
for (Character character : list2) {
System.out.print("-->"+character);
}
}
}
Vũ Việt Đức
2020-05-22 13:46: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 lession5;
/**
*
* @author ADMIN
*/
public class Test {
public static void main(String[] args) {
Thread1 t1 = new Thread1();
Thread2 t2 = new Thread2();
t1.start();
t2.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 lession5;
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;
public Thread1() {
list1 = new ArrayList<>();
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
}
Random r = new Random();
int value = r.nextInt(101);
list1.add(value);
}
for (int i = 0; i < 10; i++) {
System.out.println("Thread1: --->" + list1.get(i));
}
}
}
/*
* 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 lession5;
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;
public Thread2() {
list2 = new ArrayList<>();
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {
Logger.getLogger(Thread2.class.getName()).log(Level.SEVERE, null, ex);
}
Random r = new Random();
char value = (char)(r.nextInt(26) + 'a');
list2.add(value);
}
for (int i = 0; i < 10; i++) {
System.out.println("Thread2: --->" + list2.get(i));
}
}
}
nguyễn văn huy
2020-03-29 09:05:15
/*
* 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 thread1;
/**
*
* @author ASUS
*/
public class main {
public static void main(String[] args) {
Inter in = new Inter();
Char che = new Char(in);
in.start();
che.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 thread1;
import java.util.ArrayList;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.DoubleStream;
/**
*
* @author ASUS
*/
public class Char extends Thread{
ArrayList<Char>list2=new ArrayList<>();
Random thao=new Random();
Inter in;
public Char() {
}
public Char(Inter in) {
this.in = in;
}
@Override
public void run() {
try {
in.join();
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(2000);
int n=thao.nextInt(25)+97;
char n1=(char) n;
System.out.println("char>>>"+n1);
} catch (InterruptedException ex) {
Logger.getLogger(Char.class.getName()).log(Level.SEVERE, null, ex);
}
}
} catch (InterruptedException ex) {
Logger.getLogger(Char.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 thread1;
import java.util.ArrayList;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author ASUS
*/
public class Inter extends Thread {
ArrayList<Integer> list1=new ArrayList<>();
Random thao =new Random();
@Override
public void run() {
for (int i = 0; i < 10; i++) {
int n=thao.nextInt(100);
System.out.println("list1>>>>"+n);
list1.add(n);
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {
Logger.getLogger(Inter.class.getName()).log(Level.SEVERE, null, ex);
}
}
//To change body of generated methods, choose Tools | Templates.
}
}
Trương Công Vinh
2020-03-28 08:50:31
/*
* 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;
/**
*
* @author DELL
*/
public class Test {
public static void main(String[] args) {
Thread1 t1 = new Thread1();
Thread2 t2 = new Thread2();
t1.start();
t2.start();
try {
Thread1.sleep(11000);
t1.stop();
} catch (Exception e) {
}
try {
Thread2.sleep(11000);
t2.stop();
} catch (Exception e) {
}
}
}
/*
* 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;
import java.util.ArrayList;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author DELL
*/
public class Thread1 extends Thread {
ArrayList<Integer> list1 = new ArrayList<>();
Random random = new Random();
@Override
public void run() {
for (int i = 0;; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
}
int n = random.nextInt(100) + 1;
list1.add(n);
System.out.println("List 1 : "+(i+1)+">>>"+list1.get(i));
}
}
}
/*
* 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;
import java.util.ArrayList;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author DELL
*/
public class Thread2 extends Thread {
ArrayList<Character> list2 = new ArrayList<>();
Random random = new Random();
@Override
public void run() {
for (int i = 0;; i++) {
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {
Logger.getLogger(Thread2.class.getName()).log(Level.SEVERE, null, ex);
}
char c = (char) (random.nextInt(26) + 'a');
list2.add(c);
System.out.println("List 2 : "+(i+1)+">>>"+list2.get(i));
}
}
}
Đỗ Văn Huấn
2020-03-28 02:45:55
package java2_Advanced.BaiTapNgay27_3_2020.Thread_onLuyen;
import java.util.ArrayList;
import java.util.Random;
public class Thread1 extends Thread {
ArrayList<Integer> list1 = new ArrayList<>();
@Override
public void run() {
for (int i = 0; i < 10; i++) {
try {
Random random = new Random();
int n = random.nextInt(101);
Thread.sleep(1000);
list1.add(n);
System.out.println("T1: " + n);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
package java2_Advanced.BaiTapNgay27_3_2020.Thread_onLuyen;
import java.util.ArrayList;
import java.util.Random;
public class Thread2 extends Thread {
ArrayList<Character> list2 = new ArrayList<>();
@Override
public void run() {
for (int i = 0; i < 10; i++) {
try {
Thread2.sleep(2000);
Random a = new Random();
int char1 = a.nextInt(25) + 97;
char char2 = (char) char1;
list2.add(char2);
System.out.println("T2: " + char2);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
package java2_Advanced.BaiTapNgay27_3_2020.Thread_onLuyen;
public class Test {
public static void main(String[] args) {
Thread1 thread1= new Thread1();
Thread2 thread2 = new Thread2();
thread1.start();
try {
thread1.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
thread2.start();
}
}
thienphu
2020-03-27 09:05:06
/*
* 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 btthread;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Thien Phu
*/
public class Thread1 extends Thread {
List<Integer> list1;
public Thread1() {
list1 = new ArrayList<>();
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(1000);
Random rd = new Random();
int number = rd.nextInt(101);
list1.add(number);
System.out.println("T1:"+number);
} catch (InterruptedException ex) {
Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
}
}
System.out.println("Thread1:");
for (Integer list11 : list1) {
System.out.println(list11);
}
}
}
/*
* 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 btthread;
import java.util.ArrayList;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Thien Phu
*/
public class Thread2 extends Thread {
ArrayList<Character> list2;
public Thread2() {
list2 = new ArrayList<>();
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(2000);
Random rd = new Random();
int number = rd.nextInt(25) + 97;
list2.add((char) number);
System.out.println("T2:"+(char)number);
} catch (InterruptedException ex) {
Logger.getLogger(Thread2.class.getName()).log(Level.SEVERE, null, ex);
}
}
System.out.println("Thread2:");
for (Character list21 : list2) {
System.out.println(list21);
}
}
}
/*
* 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 btthread;
/**
*
* @author Thien Phu
*/
public class Main {
public static void main(String[] args) {
Thread1 t1 = new Thread1();
t1.start();
Thread2 t2 = new Thread2();
t2.start();
}
}