By GokiSoft.com| 19:45 15/06/2020|
Java Advanced

[Share Code] Tìm hiểu Thread trong Java, MultiThreading trong Java, Lập trình Java - Lớp C1907L

#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 lession9;

/**
 *
 * @author Diep.Tran
 */
public class Main {
    public static void main(String[] args) {
        System.out.println("Start > Main");
        //T0 >> Main
        //Phần 1: Khởi tạo Thread
        //Cách 1:
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("T1 Start");
                for (int i = 0; i < 10; i++) {
                    System.out.println("T1 > " + i);
                }
                System.out.println("T1 Stop");
            }
        });
        
        //T1 >> Main
        t1.start();
//        t1.stop();
//        t1.start(); //Chú ý : Một khi luồng stop => không thể start lại được
        //T2 >> Main, T1
        
        //Cach 2:
        NewThread t2 = new NewThread();
        //T3 >> Main, T1 (co the ton tai hoac ko)
        t2.start();
        //T4 >> Main, T1, T2 (co the ton tai hoac ko)
        
        System.out.println("Stop > Main");
    }
}


#NewThread.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 lession9;

/**
 *
 * @author Diep.Tran
 */
public class NewThread extends Thread{

    @Override
    public void run() {
        System.out.println("NewThread Start");
        for (int i = 0; i < 10; i++) {
            System.out.println("NewThread >> " + i);
        }
        System.out.println("NewThread Stop");
    }
    
}


#MultiThreadingTest.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 lession9;

/**
 *
 * @author Diep.Tran
 */
public class MultiThreadingTest {
    public static void main(String[] args) {
        SharedData sharedData = new SharedData();
        
        sharedData.addMore(1);
        sharedData.addMore(2);
        sharedData.addMore(5);
        
        //Test => CHo phep nhieu thread cung goi toi ham addMore cua object sharedData
        MultiThread t1 = new MultiThread(sharedData);
        MultiThread t2 = new MultiThread(sharedData);
        MultiThread t3 = new MultiThread(sharedData);
        MultiThread t4 = new MultiThread(sharedData);
        MultiThread t5 = new MultiThread(sharedData);
        
        t1.start();
        t2.start();
        t3.start();
        t4.start();
        t5.start();
    }
}


#MultiThread.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 lession9;

/**
 *
 * @author Diep.Tran
 */
public class MultiThread extends Thread{
    SharedData sharedData;

    public MultiThread(SharedData sharedData) {
        this.sharedData = sharedData;
    }
    
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            sharedData.addMore(i % 5);
        }
    }
    
}


#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 lession9;

/**
 *
 * @author Diep.Tran
 */
public class SharedData {
    int x;

    public SharedData() {
    }
    
    public synchronized void addMore(int delta) {
        System.out.println("x : " + x + ", delta: " + delta);
        x += delta;
        System.out.println("x: " + x);
    }
}


Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)