Synchronized 2 Thread - Bài tập mảng tên + địa chỉ - Java nâng cao
Bài 1.
Cho một danh sách là họ tên các sinh viên
Ví dụ: {“Nguyen Ngoc Lan”,”Duong Tien Nam”,”Ngo Anh Quan”,”Pham Van Duc”,”Tran Thi Ha”}
Một danh sách khác là quê tương ứng của các sinh viên trên:
Ví dụ:
{“Hải Phòng”,”Hà Nội”,”Thái Nguyên”,”Hà Tĩnh”,”Quảng Ninh”}
Viết 1 ứng dụng Java sử dụng Thread để thực hiện công việc:
- Thread thứ nhất sau mỗi giây sẽ hiển thị tên 1 người tương ứng trong danh sách tên
- Thread thứ hai ngay sau đó sẽ hiển thị quê tương ứng của người đó
- Hai Thread thực hiện mỗi thread 5 lần sẽ dừng lại
Phải đảm bảo rằng Thread1 luôn được chạy trước rồi mới đến Thread2
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-31 14:44:10
public class Main {
public static void main(String[] args) {
Information inf = new Information();
Thread1 t1 = new Thread1(inf);
Thread2 t2 = new Thread2(inf);
t1.start();
try {
Thread.sleep(50);
} catch (InterruptedException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
t2.start();
}
}
public class Information {
String[] namelist = {"nguyen Van A","Nguyen Van B","Nguyen van C","Nguyen van D","Nguyen Van E"};
String[] addresslist = {"Tien Giang","Long An","Ben tre","Tra Vinh","Can Tho"};
public Information() {
}
public String[] getNamelist() {
return namelist;
}
public void setNamelist(String[] namelist) {
this.namelist = namelist;
}
public String[] getAddresslist() {
return addresslist;
}
public void setAddresslist(String[] addresslist) {
this.addresslist = addresslist;
}
}
public class Thread1 extends Thread{
Information inf;
public Thread1(Information inf) {
this.inf = inf;
}
@Override
public void run() {
synchronized (inf) {
for (int i = 0; i < 5; i++) {
System.out.println(inf.getNamelist()[i]);
inf.notifyAll();
try {
inf.wait();
} catch (InterruptedException ex) {
Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
}
}
inf.notifyAll();
}
}
}
public class Thread2 extends Thread {
Information inf;
public Thread2(Information inf) {
this.inf = inf;
}
@Override
public void run() {
synchronized (inf) {
for (int i = 0; i < 5; i++) {
System.out.println(inf.getAddresslist()[i]);
inf.notifyAll();
try {
inf.wait();
} catch (InterruptedException ex) {
Logger.getLogger(Thread2.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
}
vuong huu phu
2021-03-21 09:16: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 javaapplication40;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Admin
*/
public class QuanLyBay {
static ArrayList<VeMayBay> air_ticket = new ArrayList<>();
static Scanner sc = new Scanner(System.in);
static ArrayList<VeMayBay> VJ = new ArrayList<>();
static ArrayList<VeMayBay> VN = new ArrayList<>();
static ArrayList<VeMayBay> JET = new ArrayList<>();
public static void main(String[] args) {
int lc;
do {
menu();
System.out.println("Nhap su lua chon");
lc = Integer.parseInt(sc.nextLine());
switch (lc) {
case 1:
nhap();
break;
case 2:
Luu();
break;
case 3:
doc();
break;
case 4:
Sapxep();
break;
case 5:
Luufile();
break;
}
} while (true);
}
public static void menu() {
System.out.println("======================================================");
System.out.println("1 Nhap thong tin cho n chuyen bay");
System.out.println("2 Luu vao file Dulieubay.txt");
System.out.println("3 Doc thong tin file Dulieubay.txt va hien thi");
System.out.println("4 Sap xep giam dan theo tung gia ve");
System.out.println("5 In thong tin cua moi hang bay ra 1 file tuong ung");
System.out.println("======================================================");
}
private static void nhap() {
int n;
System.out.println("Nhap so luong ve can them ");
n = Integer.parseInt(sc.nextLine());
for (int i = 0; i < n; i++) {
VeMayBay mb = new VeMayBay();
mb.nhap();
air_ticket.add(mb);
}
}
private static void Luu() {
System.out.println("Bat dau luu");
FileOutputStream fos = null;
try {
fos = new FileOutputStream("Dulieubay.txt");
for (VeMayBay v : air_ticket) {
String line = v.gline();
byte[] b;
b = line.getBytes("utf8");
fos.write(b);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(QuanLyBay.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(QuanLyBay.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(QuanLyBay.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException ex) {
Logger.getLogger(QuanLyBay.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
System.out.println("Luu xong");
}
private static void doc() {
FileInputStream fis = null;
InputStreamReader isr = null;
BufferedReader br = null;
try {
fis = new FileInputStream("Dulieubay.txt");
isr = new InputStreamReader(fis, StandardCharsets.UTF_8);
br = new BufferedReader(isr);
String str = null;
try {
while ((str = br.readLine()) != null) {
if (str.isEmpty()) {
continue;
}
VeMayBay mb = new VeMayBay();
mb.cu_phap(str);
air_ticket.add(mb);
}
} catch (IOException ex) {
Logger.getLogger(QuanLyBay.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(QuanLyBay.class.getName()).log(Level.SEVERE, null, ex);
}
air_ticket.forEach((veMayBay) -> {
veMayBay.Hienthi();
});
}
private static void Sapxep() {
Collections.sort(air_ticket, (VeMayBay o1, VeMayBay o2) -> {
if (o1.getFare() > o2.getFare()) {
return 1;
} else {
return -1;
}
});
air_ticket.forEach((veMayBay) -> {
veMayBay.Hienthi();
});
}
private static void Luufile() {
air_ticket.forEach((v) -> {
if (v.getFlight_code().contains("VJ")) {
VJ.add(v);
} else if (v.getFlight_code().contains("VN")) {
VN.add(v);
} else if (v.getFlight_code().contains("JET")) {
JET.add(v);
}
});
if (VJ.size() > 0) {
System.out.println("Bat dau luu VietjetAir");
FileOutputStream fos = null;
try {
fos = new FileOutputStream("VietjetAir.txt");
for (VeMayBay v : VJ) {
String line = v.gline();
byte[] b;
b = line.getBytes("utf8");
fos.write(b);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(QuanLyBay.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(QuanLyBay.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(QuanLyBay.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException ex) {
Logger.getLogger(QuanLyBay.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
if (VN.size() > 0) {
System.out.println("Bat dau luu VietNam Airline");
FileOutputStream fos = null;
try {
fos = new FileOutputStream("VietNam Airline.txt");
for (VeMayBay v : VN) {
String line = v.gline();
byte[] b;
b = line.getBytes("utf8");
fos.write(b);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(QuanLyBay.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(QuanLyBay.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(QuanLyBay.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException ex) {
Logger.getLogger(QuanLyBay.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
if (JET.size()> 0) {
System.out.println("Bat dau luu JetStar");
FileOutputStream fos = null;
try {
fos = new FileOutputStream("JetStar.txt");
for (VeMayBay v : JET) {
String line = v.gline();
byte[] b;
b = line.getBytes("utf8");
fos.write(b);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(QuanLyBay.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(QuanLyBay.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(QuanLyBay.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException ex) {
Logger.getLogger(QuanLyBay.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
System.out.println("Luu xong");
}
}
/*
* 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 javaapplication40;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
*
* @author Admin
*/
public class VeMayBay {
String Flight_code, Flight_name, Signed_luggage, strdateS, Date_of_flight;
double Fare;
public VeMayBay() {
}
public VeMayBay(String Flight_code, String Flight_name, String Signed_luggage, String Date_of_flight, double Fare) {
this.Flight_code = Flight_code;
this.Flight_name = Flight_name;
this.Signed_luggage = Signed_luggage;
this.Date_of_flight = Date_of_flight;
this.Fare = Fare;
}
public String getFlight_code() {
return Flight_code;
}
public void setFlight_code(String Flight_code) {
this.Flight_code = Flight_code;
}
public String getFlight_name() {
return Flight_name;
}
public void setFlight_name(String Flight_name) {
this.Flight_name = Flight_name;
}
public String getSigned_luggage() {
return Signed_luggage;
}
public void setSigned_luggage(String Signed_luggage) {
this.Signed_luggage = Signed_luggage;
}
public String getDate_of_flight() {
return Date_of_flight;
}
public void setDate_of_flight(String Date_of_flight) {
this.Date_of_flight = Date_of_flight;
}
public double getFare() {
return Fare;
}
public void setFare(double Fare) {
this.Fare = Fare;
}
@Override
public String toString() {
return "Flight code =" + Flight_code + ", Flight name =" + Flight_name + ", Signed luggage =" + Signed_luggage + ", Date of flight =" + Date_of_flight + ", Fare =" + Fare;
}
public void nhap() {
Scanner sc = new Scanner(System.in);
System.out.println("");
System.out.println("Nhap ma chuyen bay:");
while (true) {
Flight_code = sc.nextLine();
String exp = "";
if (Flight_code.startsWith("VJ")) {
exp = "^VJ[1-9]{1}[0-9]{1}$";
} else if (Flight_code.startsWith("VN")) {
exp = "^VN[1-9]{1}[0-9]{1}[0-9]?$";
} else if (Flight_code.startsWith("JET")) {
exp = "^JET[1-9]{1}[0-9]{1}$";
}else {
System.out.println("Nhap lai !!!!!");
continue;
}
Pattern pt = Pattern.compile(exp);
Matcher mc = pt.matcher(Flight_code);
if (mc.find()) {
break;
}else{
System.out.println("Nhap lai!!!");
}
}
System.out.println("Nhap ten chuyen bay:");
Flight_name = sc.nextLine();
System.out.println("Nhap ngay bay:");
Date_of_flight = sc.nextLine();
System.out.println("Nhap hanh li kinh gui:");
Signed_luggage = sc.nextLine();
System.out.println("Nhap gia ve:");
Fare = Double.parseDouble(sc.nextLine());
System.out.println("");
}
public void Hienthi() {
System.out.println(this);
}
public String gline() {
return "Flight code =" + Flight_code + ", Flight name =" + Flight_name + ", Signed luggage =" + Signed_luggage + ", Date of flight =" + Date_of_flight + ", Fare =" + Fare;
}
public void cu_phap(String line) {
String[] str = line.split(",");
try {
Flight_code = str[0];
Flight_name = str[1];
Date_of_flight = str[2];
Signed_luggage = str[3];
Fare = Double.parseDouble(str[4]);
} catch (Exception e) {
} finally {
}
}
}
vuong huu phu
2021-03-19 13:08: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 javaapplication38;
/**
*
* @author Admin
*/
public class Test {
public static void main(String[] args) {
SharedData sr = new SharedData();
Thread1 t1=new Thread1(sr);
Thread2 t2 = new Thread2(sr);
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 javaapplication38;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Admin
*/
public class Thread1 extends Thread {
SharedData sr;
public Thread1(SharedData sr) {
this.sr = sr;
}
@Override
public void run() {
try {
sleep(10);
} catch (InterruptedException ex) {
Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
}
synchronized(sr){
for (int i = 0; i < 5; i++) {
System.out.println("Ho va ten: "+sr.getName()[i]);
sr.notifyAll();
try {
sr.wait();
} catch (InterruptedException ex) {
Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
}
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 javaapplication38;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Admin
*/
public class Thread2 extends Thread{
SharedData sr;
public Thread2(SharedData sr) {
this.sr = sr;
}
@Override
public void run() {
synchronized(sr){
for (int i = 0; i < 5; i++) {
try {
sr.wait();
} catch (InterruptedException ex) {
Logger.getLogger(Thread2.class.getName()).log(Level.SEVERE, null, ex);
}
sr.notifyAll();
System.out.println("Que quan: "+sr.getQue_quan()[i]);
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 javaapplication38;
import java.util.ArrayList;
/**
*
* @author Admin
*/
public class SharedData {
String[] Name = {"Nguyen Ngoc Lan","Duong Tien Nam","Ngo Anh Quan","Pham Van Duc","Tran Thi Ha"};
String[] Que_quan = {"Hai Phong","Ha Noi","Thai Nguyen","Ha Tinh","Quang Ninh"};
public String[] getName() {
return Name;
}
public void setName(String[] Name) {
this.Name = Name;
}
public String[] getQue_quan() {
return Que_quan;
}
public void setQue_quan(String[] Que_quan) {
this.Que_quan = Que_quan;
}
}
Đỗ Văn Huấn
2020-04-02 04:10:46
bai2
package java2_Advanced.BaiTapNgay27_3_2020.OnLuyenFile_MultiThread.Bai2;
import java.io.Serializable;
import java.util.Scanner;
import java.util.regex.Pattern;
public class VeMayBay implements Serializable {
String ma, tenChuyenBay, ngayBay, hanhLyKiGui;
float giave;
public VeMayBay() {
}
public VeMayBay(String ma, String tenChuyenBay, String ngayBay, String hanhLyKiGui, Float giave) {
this.ma = ma;
this.tenChuyenBay = tenChuyenBay;
this.ngayBay = ngayBay;
this.hanhLyKiGui = hanhLyKiGui;
this.giave = giave;
}
public String getMa() {
return ma;
}
public void setMa(String ma) {
this.ma = ma;
}
public String getTenChuyenBay() {
return tenChuyenBay;
}
public void setTenChuyenBay(String tenChuyenBay) {
this.tenChuyenBay = tenChuyenBay;
}
public String getNgayBay() {
return ngayBay;
}
public void setNgayBay(String ngayBay) {
this.ngayBay = ngayBay;
}
public String getHanhLyKiGui() {
return hanhLyKiGui;
}
public void setHanhLyKiGui(String hanhLyKiGui) {
this.hanhLyKiGui = hanhLyKiGui;
}
public float getGiave() {
return giave;
}
public void setGiave(float giave) {
this.giave = giave;
}
public static boolean checkMa1(String roll) {
String VJABB = "[V][J][1-9][0-9]{2}";
boolean check1 = Pattern.matches(VJABB, roll);
return check1;
}
public static boolean checkMa2(String roll) {
String VNABBC = "[V][N][1-9][0-9]{2}[0-9]?";
boolean check2 = Pattern.matches(VNABBC, roll);
return check2;
}
public static boolean checkMa3(String roll) {
String JETABB = "[J][E][T][0-9][0-9]{2}";
boolean check3 = Pattern.matches(JETABB, roll);
return check3;
}
public void nhap() {
Scanner scanner = new Scanner(System.in);
System.out.println("Nhap tên chuyen bay: ");
tenChuyenBay = scanner.nextLine();
System.out.println("Nhap ngay bay");
ngayBay = scanner.nextLine();
System.out.println("Nhap hanh ly ki gui: ");
hanhLyKiGui = scanner.nextLine();
System.out.println("Nhap gia ve: ");
giave = Float.parseFloat(scanner.nextLine());
}
@Override
public String toString() {
return "VeMayBay{" +
"ma='" + ma + '\'' +
", tenChuyenBay='" + tenChuyenBay + '\'' +
", ngayBay='" + ngayBay + '\'' +
", hanhLyKiGui='" + hanhLyKiGui + '\'' +
", giave='" + giave + '\'' +
'}';
}
public void display() {
System.out.println(toString());
}
public String getline() {
return ma + ", " + tenChuyenBay + ", " + ngayBay + ", " + hanhLyKiGui + ", " + giave + "\n";
}
}
package java2_Advanced.BaiTapNgay27_3_2020.OnLuyenFile_MultiThread.Bai2;
import java.io.*;
import java.util.*;
public class Abc {
List<VeMayBay> list = new ArrayList<>();
Scanner scan = new Scanner(System.in);
VeMayBay vmb = new VeMayBay();
public void input() {
System.out.println("Nhap so n chuyen bay: ");
int n = Integer.parseInt(scan.nextLine());
for (int i = 0; i < n; i++) {
VeMayBay vmb = new VeMayBay();
list.add(vmb);
System.out.println("Nhap thong tin chuyen bay thu " + (i + 1) + " :");
System.out.println("Chon chuyen bay: ");
int choose;
System.out.println("1. chuyến bay của hãng VietjetAir");
System.out.println("2. chuyến bay của hãng VietNam Airline");
System.out.println("3. chuyến bay của hãng JetStar");
System.out.println("chon: ");
choose = Integer.parseInt(scan.nextLine());
switch (choose) {
case 1:
System.out.println("Nhap ma");
vmb.ma = scan.nextLine();
if (vmb.checkMa1(vmb.ma) == false) {
while (!vmb.checkMa1(vmb.ma)) {
System.out.println("Nhap lai: ");
vmb.ma = scan.nextLine();
if (vmb.checkMa1(vmb.ma)) {
break;
}
}
}
break;
case 2:
System.out.println("Nhap ma:");
vmb.ma = scan.nextLine();
if (vmb.checkMa2(vmb.ma) == false) {
while (!vmb.checkMa2(vmb.ma)) {
System.out.println("Nhap lai: ");
vmb.ma = scan.nextLine();
if (vmb.checkMa2(vmb.ma)) {
break;
}
}
}
break;
case 3:
System.out.println("Nhap ma: ");
vmb.ma = scan.nextLine();
if (vmb.checkMa3(vmb.ma) == false) {
while (!vmb.checkMa3(vmb.ma)) {
System.out.println("Nhap lai: ");
vmb.ma = scan.nextLine();
if (vmb.checkMa3(vmb.ma)) {
break;
}
}
}
break;
default:
System.err.println("Nhap loi !!!");
break;
}
vmb.nhap();
}
}
public void hienthi() {
for (VeMayBay a : list) {
a.display();
}
}
public void saveDuLieuBay() {
FileOutputStream fos = null;
BufferedOutputStream bos = null;
try {
fos = new FileOutputStream("d:/Admin/Java2_advanced/duLieuBay.txt");
bos = new BufferedOutputStream(fos);
for (VeMayBay ve : list) {
String line = ve.getline();
byte[] bytes = line.getBytes("UTF-8");
bos.write(bytes);
}
System.out.println("Ghi thanh cong.");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public void readDuLieuBay() {
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader("d:/Admin/Java2_advanced/duLieuBay.txt"));
String a = br.readLine();
while (a != null) {
System.out.println(a);
a = br.readLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public void sortGiaVe() {
Collections.sort(list, new Comparator<VeMayBay>() {
@Override
public int compare(VeMayBay veMayBay, VeMayBay t1) {
if (veMayBay.giave > t1.giave) {
return -1;
}
return 1;
}
});
for (VeMayBay a : list) {
a.display();
}
}
public void SaveFile_Ma() {
for (int j = 0; j < list.size(); j++) {
if (vmb.checkMa1(list.get(j).ma) == true) {
FileOutputStream fos = null;
BufferedOutputStream bos = null;
try {
fos = new FileOutputStream("d:/Admin/Java2_advanced/ma1.txt");
bos = new BufferedOutputStream(fos);
for (int i = 0; i < list.size(); i++) {
if (vmb.checkMa1(list.get(i).ma) == true) {
String line = list.get(i).getline();
byte[] bytes = line.getBytes("UTF-8");
bos.write(bytes);
}
}
System.out.println("Ghi thanh cong.");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} else if (vmb.checkMa2(list.get(j).ma) == true) {
FileOutputStream fos = null;
BufferedOutputStream bos = null;
try {
fos = new FileOutputStream("d:/Admin/Java2_advanced/ma2.txt");
bos = new BufferedOutputStream(fos);
for (int i = 0; i < list.size(); i++) {
if (vmb.checkMa2(list.get(i).ma) == true) {
String line = list.get(i).getline();
byte[] bytes = line.getBytes("UTF-8");
bos.write(bytes);
}
}
System.out.println("Ghi thanh cong.");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} else {
FileOutputStream fos = null;
BufferedOutputStream bos = null;
try {
fos = new FileOutputStream("d:/Admin/Java2_advanced/ma3.txt");
bos = new BufferedOutputStream(fos);
for (int i = 0; i < list.size(); i++) {
if (vmb.checkMa3(list.get(i).ma) == true) {
String line = list.get(i).getline();
byte[] bytes = line.getBytes("UTF-8");
bos.write(bytes);
}
}
System.out.println("Ghi thanh cong.");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}
}
package java2_Advanced.BaiTapNgay27_3_2020.OnLuyenFile_MultiThread.Bai2;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Abc abc = new Abc();
Scanner scan = new Scanner(System.in);
int choose;
do {
showMenu();
System.out.println("Nhap choose:");
choose = Integer.parseInt(scan.nextLine());
switch (choose) {
case 1:
abc.input();
break;
case 2:
abc.saveDuLieuBay();
break;
case 3:
abc.readDuLieuBay();
break;
case 4:
abc.sortGiaVe();
break;
case 5:
abc.SaveFile_Ma();
break;
default:
System.err.println("Nhap sai !!!");
break;
}
} while (choose != 5);
}
static void showMenu() {
System.out.println("1.Nhập vào thông tin cho n chuyến bay với mã chuyến bay theo quy ước");
System.out.println("2.Lưu thông tin vừa nhập vào file : DuLieuBay.txt");
System.out.println("3.Đọc thông tin từ DuLieuBay.txt và hiển thị tất cả thông tin ra màn hình");
System.out.println("4.Hiển thị thong tin sau khi đã sắp xếp giảm dần theo giá vé.");
System.out.println("5.In ra thông tin của mỗi hãng bay ra một file tương ứng.");
}
}
bai1package java2_Advanced.BaiTapNgay27_3_2020.OnLuyenFile_MultiThread.Bai1;
public class Data {
String[] t2 = {"Hai Phong","Ha Noi", "Thai Nguyen","Ha Tinh","Quang Ninh"};
String[] t1 = { "Nguyen Ngoc Lan", "Duong Tien Nam","Ngo Anh Quan","Pham Van Duc","Tran Thi Ha"};
public String[] getT2() {
return t2;
}
public void setT2(String[] t2) {
this.t2 = t2;
}
public String[] getT1() {
return t1;
}
public void setT1(String[] t1) {
this.t1 = t1;
}
}
package java2_Advanced.BaiTapNgay27_3_2020.OnLuyenFile_MultiThread.Bai1;
public class Main {
public static void main(String[] args) {
Data data = new Data();
T1 t1= new T1(data);
T2 t2 = new T2(data);
t1.start();
t2.start();
}
}
package java2_Advanced.BaiTapNgay27_3_2020.OnLuyenFile_MultiThread.Bai1;
public class T1 extends Thread {
Data data;
public T1(Data data) {
this.data = data;
}
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0; i < 5; i++) {
synchronized (data) {
System.out.println("Ho va ten: " + data.t1[i]);
try {
data.notifyAll();
data.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
synchronized (data) {
data.notifyAll();
}
}
}
package java2_Advanced.BaiTapNgay27_3_2020.OnLuyenFile_MultiThread.Bai1;
public class T2 extends Thread {
Data data;
public T2(Data data) {
this.data = data;
}
@Override
public void run() {
for (int i = 0; i < data.t2.length; i++) {
synchronized (data) {
try {
data.notifyAll();
data.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Dia chi: " + data.t2[i]);
}
}
synchronized (data) {
data.notifyAll();
}
}
}
thienphu
2020-03-31 14:36: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 Thread3;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author Thien Phu
*/
public class ShareData {
List<String> danhsach;
public ShareData() {
danhsach = new ArrayList<>();
}
public List<String> getDanhsach() {
return danhsach;
}
public void setDanhsach(List<String> danhsach) {
this.danhsach = danhsach;
}
public void addListName() {
danhsach.add("Nguyen Ngoc Lan");
danhsach.add("Duong Tien Nam");
danhsach.add("Ngo Anh Quan");
danhsach.add("Pham Van Duc");
danhsach.add("Tran Thi Ha");
danhsach.add("Hai Phong");
danhsach.add("Ha Noi");
danhsach.add("Thai Nguyen");
danhsach.add("Ha Tinh");
danhsach.add("Quang Ninh");
}
}
/*
* 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 Thread3;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Thien Phu
*/
public class Thread1 extends Thread {
ShareData shareData;
public Thread1(ShareData shareData) {
this.shareData = shareData;
}
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
}
synchronized (shareData) {
for (int i = 0; i < 5; i++) {
System.out.println("Name: " + shareData.danhsach.get(i));
shareData.notifyAll();
try {
shareData.wait();
} catch (InterruptedException ex) {
Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
}
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
}
}
System.out.println("Stop t1");
synchronized(shareData){
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 Thread3;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Thien Phu
*/
public class Thread2 extends Thread {
ShareData shareData;
public Thread2(ShareData shareData) {
this.shareData = shareData;
}
@Override
public void run() {
synchronized (shareData) {
for (int i = 5; i < 10; i++) {
shareData.notifyAll();
try {
shareData.wait();
} catch (InterruptedException ex) {
Logger.getLogger(Thread2.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("Address: " + shareData.danhsach.get(i));
}
System.out.println("T2: Stop");
synchronized (shareData) {
shareData.notifyAll();
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 Thread3;
/**
*
* @author Thien Phu
*/
public class Main {
public static void main(String[] args) {
ShareData shareData = new ShareData();
shareData.addListName();
Thread1 t1 = new Thread1(shareData);
Thread2 t2 = new Thread2(shareData);
t2.start();
t1.start();
}
}
Trương Công Vinh
2020-03-31 06:04:02
Bai 1
/*
* 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 Practice1;
import java.util.ArrayList;
/**
*
* @author DELL
*/
public class ShareData {
int index =1;
ArrayList<String> list1 = new ArrayList<>();
ArrayList<String> list2 = new ArrayList<>();
public ArrayList<String> getList1() {
return list1;
}
public void setList1(ArrayList<String> list1) {
this.list1 = list1;
}
public ArrayList<String> getList2() {
return list2;
}
public void setList2(ArrayList<String> list2) {
this.list2 = list2;
}
public ShareData() {
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
}
/*
* 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 Practice1;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author DELL
*/
public class Thread1 extends Thread {
ShareData sharedata;
public Thread1(ShareData sharedata) {
this.sharedata = sharedata;
}
@Override
public void run() {
for (int i = 0; i < 5; i++) {
synchronized (sharedata) {
System.out.println("Name : " + sharedata.getList1().get(i));
sharedata.setIndex(2);
sharedata.notifyAll();
try {
sharedata.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 Practice1;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author DELL
*/
public class Thread2 extends Thread {
ShareData sharedata;
public Thread2(ShareData sharedata) {
this.sharedata = sharedata;
}
@Override
public void run() {
for (int i = 0; i < 5; i++) {
synchronized (sharedata) {
sharedata.notifyAll();
try {
while (sharedata.getIndex()!= 2) {
sharedata.wait();
}
} catch (InterruptedException ex) {
Logger.getLogger(Thread2.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("Address : " + sharedata.getList2().get(i));
sharedata.setIndex(1);
}
}
synchronized(sharedata){
sharedata.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 Practice1;
import java.util.ArrayList;
/**
*
* @author DELL
*/
public class Main {
public static void main(String[] args) {
ArrayList<String> list1 = new ArrayList<String>();
ArrayList<String> list2 = new ArrayList<String>();
ShareData sharedata = new ShareData();
setList(list1, list2);
sharedata.setList1(list1);
sharedata.setList2(list2);
Thread1 t1 = new Thread1(sharedata);
t1.start();
Thread2 t2 = new Thread2(sharedata);
t2.start();
}
static void setList(ArrayList<String> list1, ArrayList<String> list2) {
list1.add("Nguyen Ngoc Lan");
list1.add("Duong Tien Nam");
list1.add("Ngo Anh Quan");
list1.add("Pham Van Duc");
list1.add("Tran Thi Ha");
list2.add("Hải Phòng");
list2.add("Hà Nội");
list2.add("Thái Nguyên");
list2.add("Hà Tĩnh");
list2.add("Quảng Ninh");
}
}