Tao tác dữ liệu lên Shared Object sử dụng MultiThreading trong java
Viết chương trình thực hiện yêu cầu sau
- Thiết kế 1 class SharedData có chứa biến nguyên total
- Thiết kế thread 1 thực hiện sinh ngẫu nhiên các số từ 0 - 100 -> Sau đó thêm số ngẫu nhiên này vào biến total trong SharedData (Ví dụ : Số ngẫu nhiên rad khi đó total = total + rad)
- Thiết kế thread 2 thực hiện sinh ngẫu nhiên các số từ -100 - 0 -> Sau đó thêm số ngẫu nhiên này vào biến total trong SharedData (Ví dụ : Số ngẫu nhiên rad khi đó total = total + rad)
Khi giá trij total của SharedData <= -100 hoặc >= 100 thì thực hiện dừng 2 thread trên.
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)
Nguyễn Chí Tâm
2022-12-30 17:22:37
public class Main {
public static void main(String[] args) {
Share share = new Share();
Thread1 t1 = new Thread1(share);
Thread2 t2 = new Thread2(share);
t2.start();
t1.start();
}
public class Share {
int total=0;
boolean check = true;
public Share() {
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public boolean isCheck() {
return check;
}
public void setCheck(boolean check) {
this.check = check;
}
public synchronized void sum(int input){
total += input;
if(total<=-100||total>=100){
check = false;
System.out.println(total+" STOP");
}else System.out.println(total);
}
}
public class Thread1 extends Thread{
Share share;
Random random;
public Thread1(Share share) {
this.share = share;
}
@Override
public void run() {
while(share.isCheck()){
random= new Random();
int rad = random.nextInt(50);
share.sum(rad);
}
}
}
public class Thread2 extends Thread{
Share share;
Random random;
public Thread2(Share share) {
this.share = share;
}
@Override
public void run() {
while(share.isCheck()){
random = new Random();
int rad = random.nextInt(50)-50;
share.sum(rad);
}
}
}
Triệu Văn Lăng
2021-03-26 08:58:00
/*
* 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 bai1106;
import java.util.Random;
/**
*
* @author MTLS
*/
public class Thread2 extends Thread {
SharedData sd;
public Thread2(SharedData sd) {
this.sd = sd;
}
@Override
public void run() {
Random rd = new Random();
int total;
for (int i = 0; i > -1; i++) {
int rad = -rd.nextInt(100);
sd.addTotal(rad);
System.out.println("Random Thread 2: " + rad);
if (sd.getTotal() <= -100 || sd.getTotal() >= 100) {
stop();
}
}
}
}
Triệu Văn Lăng
2021-03-26 08:57: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 bai1106;
import java.util.Random;
/**
*
* @author MTLS
*/
public class Thread1 extends Thread {
SharedData sd;
public Thread1(SharedData sd) {
this.sd = sd;
}
@Override
public void run() {
Random rd = new Random();
int total = 0;
for (int i = 0; i > -1; i++) {
int rad = rd.nextInt(100);
sd.addTotal(rad);
System.out.println("Random Thread 1: " + rad);
if (sd.getTotal() >= 100 || sd.getTotal() <= -100) {
stop();
}
}
}
}
Triệu Văn Lăng
2021-03-26 08:57: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 bai1106;
/**
*
* @author MTLS
*/
public class SharedData {
int total;
public SharedData() {
}
public SharedData(int total) {
this.total = total;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public void addTotal(int x) {
total +=x;
}
}
Triệu Văn Lăng
2021-03-26 08:56:57
/*
* 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 bai1106;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author MTLS
*/
public class Main {
public static void main(String[] args) {
SharedData sd = new SharedData();
Thread1 t1 = new Thread1(sd);
Thread2 t2 = new Thread2(sd);
t1.start();
try {
t1.join();
} catch (InterruptedException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
t2.start();
try {
t2.join();
} catch (InterruptedException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("Tong 2 Thread: " + sd.total);
}
}
Nguyễn Tiến Đạt
2021-03-20 04:34:20
#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.LuyenThread.ThaoTacLenShareObject;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author MyPC
*/
public class Main {
public static void main(String[] args) {
SharedData data = new SharedData();
Thread1 t1 = new Thread1(data);
Thread2 t2 = new Thread2(data);
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(data.total);
}
}
#SharedData.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.LuyenThread.ThaoTacLenShareObject;
/**
*
* @author MyPC
*/
public class SharedData {
int total = 0;
public void add(int a){
total += a;
}
}
#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.LuyenThread.ThaoTacLenShareObject;
import java.util.Random;
/**
*
* @author MyPC
*/
public class Thread1 extends Thread{
SharedData data;
public Thread1(SharedData data) {
this.data = data;
}
@Override
public void run() {
Random random = new Random();
while(data.total > -100 && data.total < 100){
int t = random.nextInt(101);
data.add(t);
}
}
}
#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.LuyenThread.ThaoTacLenShareObject;
import java.util.Random;
/**
*
* @author MyPC
*/
public class Thread2 extends Thread{
SharedData data;
public Thread2(SharedData data) {
this.data = data;
}
@Override
public void run() {
Random random = new Random();
while(data.total > -100 && data.total < 100){
int t = -random.nextInt(101);
data.add(t);
}
}
}
vuong huu phu
2021-03-19 09:39:51
/*
* 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 javaapplication39;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Admin
*/
public class JavaApplication39 {
public static void main(String[] args) {
SharedData sr= new SharedData();
Thread1 t1 = new Thread1(sr);
Thread2 t2 = new Thread2(sr);
t1.start();
try {
t1.join();
} catch (InterruptedException ex) {
Logger.getLogger(JavaApplication39.class.getName()).log(Level.SEVERE, null, ex);
}
t2.start();
try {
t2.join();
} catch (InterruptedException ex) {
Logger.getLogger(JavaApplication39.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 javaapplication39;
import java.util.Random;
/**
*
* @author Admin
*/
public class Thread1 extends Thread{
SharedData sharedData;
public Thread1(SharedData sharedData) {
this.sharedData = sharedData;
}
@Override
public void run() {
Random random = new Random();
for (int i = 0; i > -1; i++) {
int rad = random.nextInt(100);
int total=0;
total =sharedData.getTotal() + rad;
sharedData.setTotal(total);
System.out.println("T1 = "+total);
if (sharedData.getTotal() <= -100 || sharedData.getTotal() >= 100){
stop();
}
}
}
}
/*
* 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 javaapplication39;
import java.util.Random;
/**
*
* @author Admin
*/
public class Thread2 extends Thread{
SharedData sharedData;
public Thread2(SharedData sharedData) {
this.sharedData = sharedData;
}
@Override
public void run() {
Random random = new Random();
for (int i = 0; i > -1; i++) {
int rad = -random.nextInt(100);
int total=0;
total =sharedData.getTotal() + rad;
sharedData.setTotal(total);
System.out.println("T2 = "+total);
if (sharedData.getTotal() <= -100 || sharedData.getTotal() >= 100){
stop();
}
}
}
}
/*
* 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 javaapplication39;
/**
*
* @author Admin
*/
public class SharedData {
int total;
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
}
Trần Thị Khánh Huyền
2021-03-19 09:19:33
/*
* 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 Bt1106;
import java.util.Random;
/**
*
* @author Admin
*/
public class Thread2 extends Thread {
SharedData sharedData;
public Thread2(SharedData shareddata) {
this.sharedData = shareddata;
}
@Override
public void run(){
Random random = new Random();
while(sharedData.checkTotal()){
int rad = sharedData.getRandomNumber(-100,0);
sharedData.total(rad);
System.out.println("so random la: "+rad+", tong la: "+sharedData.getTotal());
}
}
Trần Thị Khánh Huyền
2021-03-19 09:19:12
/*
* 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 Bt1106;
import java.util.Random;
/**
*
* @author Admin
*/
public class Thread1 extends Thread {
SharedData sharedData;
public Thread1(SharedData sharedData) {
this.sharedData = sharedData;
}
@Override
public void run(){
Random random = new Random();
while (sharedData.checkTotal()){
int rad = sharedData.getRandomNumber(0,100);
sharedData.total(rad);
System.out.println("so random la: "+rad+", tong la: "+sharedData.getTotal());
}
}
}
Trần Thị Khánh Huyền
2021-03-19 09:18:11
/*
* 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 Bt1106;
import java.util.Random;
/**
*
* @author Admin
*/
public class SharedData {
int total;
public SharedData() {
}
public SharedData(int total) {
this.total = total;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public synchronized void total(int rad){
total=total+rad;
}
public synchronized boolean checkTotal(){
if ((total>=100)||(total<=-100)){
return false;
}return true;
}
public static int getRandomNumber (int min, int max)
{
Random random = new Random();
return random.ints(min,(max+1)).findFirst().getAsInt();
}
}