Main content:
      An introduction to threads
      How to create threads
      How to manipulate threads
      How to synchronize threads

      The Order Queue application
-------------------
1. An introduction to threads



2. How to create threads


   Ví dụ:

3. How to manipulate threads


4. How to synchronize threads

5. The Order Queue application
Bài tập:
1. Dùng thread viết chương trình cứ mỗi giây in giá trị một biến đếm tăng dần ra màn hình. Ví dụ bên dưới:
Dem: 0
Dem: 1
Dem: 2
Dem: 3
...
Mã nguồn:
Cách 1: extends từ Thread
public class MainClass {
       public static void main(String[] args) {
              Thread t2 = new IOThread();
              t2.start();
       }
}

class IOThread extends Thread {
       int dem = 0;
       public void run() {
              while (true) {
                     System.out.println("Dem: " + dem++);
                     try {
                           Thread.sleep(1000);
                     } catch (InterruptedException e) {
                           e.printStackTrace();
                     }
              }
       }
}

Cách 2: implements từ Runnable
public class MainClass {
       public static void main(String[] args) {
              Thread t = new Thread (new IOTask());
              t.start();
       }
}

class IOTask implements Runnable {
       int dem = 0;
       public void run() {
              while(true) {
                     System.out.println("Dem:" + dem++);
                     try {
                           Thread.sleep(1000);
                     } catch (InterruptedException e) {
                           e.printStackTrace();
                     }
              }
       }
}

2. Viết chương trình tạo 2 thread:
- Với thread thứ nhất sau mỗi giây in ra giá trị của một số nguyên tăng dần (int count1).
- Với thread thứ hai sau mỗi giây in ra 2 lần giá trị của một số nguyên tăng dần (int count2).
Kết quả in ra màn hình như sau:

Thread 1: count1 = 0 Thread 2: count2 = 0 Thread 2: count2 = 1 Thread 1: count1 = 1 Thread 2: count2 = 2 Thread 2: count2 = 3 Thread 1: count1 = 2 Thread 2: count2 = 4 Thread 2: count2 = 5
...

Mã nguồn:
public class MainClass {
       public static void main(String[] args) {
              Thread1 t1 = new Thread1();
              t1.start();
             
              Thread2 t2 = new Thread2();
              t2.start();
       }
}

class Thread1 extends Thread {
       int count1 = 0;
       public void run() {
              while (true) {
                     System.out.println("Thread 1: count1 = " + count1++);
                     try {
                           Thread.sleep(1000);
                     } catch (InterruptedException e) {
                           e.printStackTrace();
                     }
              }
       }
}

class Thread2 extends Thread {
       int count2 = 0;
       public void run() {
              while(true) {
                     System.out.println("    Thread 2: count2 = " + count2++);
                     try {
                           Thread.sleep(500);
                     } catch (InterruptedException e) {
                           e.printStackTrace();
                     }
              }
       }
}

3. Viết chương trình demo sử dụng synchronized:

Mã nguồn:
//example of java synchronized method 
class Table {
       synchronized void printTable(int n) {// synchronized method
              for (int i = 1; i <= 5; i++) {
                     System.out.println(n * i);
                     try {
                           Thread.sleep(400);
                     } catch (Exception e) {
                           System.out.println(e);
                     }
              }
       }
}

class MyThread1 extends Thread {
       Table t;

       MyThread1(Table t) {
              this.t = t;
       }

       public void run() {
              t.printTable(5);
       }

}

class MyThread2 extends Thread {
       Table t;

       MyThread2(Table t) {
              this.t = t;
       }

       public void run() {
              t.printTable(100);
       }
}

public class Main {
       public static void main(String args[]) {
              Table obj = new Table();// only one object
              MyThread1 t1 = new MyThread1(obj);
              MyThread2 t2 = new MyThread2(obj);
              t1.start();
              t2.start();
       }
}

4.
Main content:
      Introduction to XML
      How to view and edit an XML file
      An introduction to three XML API
      How to use stAX to work with XML
------------------
1. Introduction to XML
   - XML, hoặc Extensible Markup Language (ngôn ngữ đánh dấu mở rộng), là một ngôn ngữ đánh dấu mà bạn có thể sử dụng để tạo ra thẻ riêng của mình.
- XML attributes (thuộc tính XML):
  Thuộc tính phải nằm trong dấu trích dẫn, có hai nguyên tắc về các thuộc tính trong văn bản XML:
  + Thuộc tính phải có giá trị
  + Những giá trị đó phải được đặt trong dấu trích dẫn (" hoặc ')
  Ví dụ:


2. How to view and edit an XML file

Bài tập:
1. 

2. 

3.

Practice – (Chapter 18)
Main content:
      Introduction to directories and files
      Introduction to file input and output
      How to work with text files
      How to work with binary files
      How to work with random-access files

Bài tập:
1. Đọc tệp tin text.txt và in nội dung ra màn hình.
Mã nguồn:
import java.io.FileReader;
public class MainClass {
   public static void main(String[] argv) throws Exception {
                 FileReader fr = new FileReader("C:\\text.txt");
                 int count;
                 char chrs[] = new char[80];
  
                 do {
                              count = fr.read(chrs);
                              for (int i = 0; i < count; i++) {
                                            System.out.print(chrs[i]);
                              }
                 } while (count != -1);
   }
}
2.Đọc tệp tin text.txt theo từng dòng và in nội dung ra màn hình
Mã nguồn:
import java.io.FileReader;
import java.io.BufferedReader;
public class Main {
          public static void main(String[] argv) throws Exception {
                       FileReader fr = new FileReader("C:\\text.txt");
                       BufferedReader br = new BufferedReader(fr);
         
                       String eachLine = br.readLine();
         
                       while (eachLine != null) {
                                     if (eachLine != null) {
                                                   System.out.println(eachLine);
                                     }
                                     eachLine = br.readLine();
                       }
                       br.close();
          }
}
3. Đọc mảng số nguyên hai chiều được lưu trong tệp tin mang.txt vào mảng số nguyên a[][] và in mảng này a[][] ra màn hình. Mảng hai chiều lưu trong tệp tin mang.txt theo quy tắc sau: dòng đầu ghi 2 số n và m lần lượt số dòng và số cột của ma trận, n dòng tiếp theo lưu n dòng của ma trận, các số cách nhau 1 khoảng trống. Ví dụ mảng A[3][4] được lưu trong tệp tin mang.txt như sau:


Mã nguồn:
import java.io.BufferedReader;
import java.io.FileReader;
public class DocFile {
                 public static void main(String[] args) throws Exception {
                              FileReader fr = new FileReader("C:\\text.txt");//Tạo FileREader
                              BufferedReader br = new BufferedReader(fr); //Tạo BufferReader
                             
                              String line = "";
                              line = br.readLine();//dòng đầu tiên
                              String val[] = line.split(" ");
                              int n = Integer.parseInt(val[0]);
                              int m = Integer.parseInt(val[1]);
                              int a[][] = new int[n][m];
                             
                              for (int i = 0; i < n; i++) {
                                            line = br.readLine();
                                            if (line != null) {
                                                          val = line.split(" ");
                                                          for(int j = 0; j < m; j++) {
                                                                       a[i][j] = Integer.parseInt(val[j]);
                                                          }
                                            }
                              }
                              br.close();
                              InMang(a, n, m);
          }
         
          //In mảng hai chiều ra màn hình
          public static void InMang(int A[][], int n, int m) {
                       System.out.println(n+ " " + m);
                       for(int i = 0; i < n; i++) {
                                     for (int j = 0; j < m; j++) {
                                                   System.out.format("% 3d", A[i][j]);
                                     }
                                     System.out.println();
                       }
          }
}

4. Tạo mảng 2 chiều với giá trị nguyên ngẫu nhiên (dùng Math.random()), với số dòng n và số cột m được nhập từ bàn phím. Sau đó ghi giá trị của mảng này vào tệp tin Output.txt theo quý tắc sau: dòng đầu ghi 2 số n và m lần lượt số dòng và số cột của ma trận, n dòng tiếp theo lưu n dòng của ma trận, các số cách nhau 1 khoảng trống. Ví dụ mảng A[3][4] được lưu trong tệp tin Output.txt như sau:
Mã nguồn:

import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class Main {
       public static void main(String[] args) {
              Scanner scanner = new Scanner(System.in);
              int n, m;
              System.out.print("Nhap n, m: ");
              n = scanner.nextInt();
              m = scanner.nextInt();
             
              int a[][] = new int[n][m];

              //Tạo mảng random         
              for (int i = 0; i < n; i++) {
                     for (int j = 0; j < m; j++) {
                           a[i][j] = (int) (Math.random() * 100);
                     }
              }

              //Ghi mảng a[n][m] vào tệp tin
              try {
                     FileWriter fw = new FileWriter("output.txt");
                     fw.write(n + " " + m + "\n");//Ghi dòng đầu gồm 2 số n và m
                    
                     for (int i = 0; i < n; i++) {
                           for (int j = 0; j < m; j++) {
                                  fw.write(Integer.toString(a[i][j]) + " ");
                           }
                           fw.write("\n");
                     }
                     fw.close();
              } catch (IOException e) {
                     e.printStackTrace();
              }
       }
}

5. Viết chương trình cho phép lưu các đối tượng SinhVien (Mỗi đối tượng sinh viên gồm có trường: tên và tuổi) được nhập từ bàn phím vào tệp tin sinhvien.dat, sau đó đọc tất cả các đối tượng sinh viên được lưu trong tệp tin sinhvien.dat ra màn hình.


Mã nguồn: