By GokiSoft.com| 15:34 04/11/2022|
Java Advanced

[Source Code] Tìm hiểu Thread trong Java - C2109I

10 app chay song song:
	app : danh sach command line -> process

May tinh: HDH
	CPU:
		1 CPU ~ 10 app
			1CPU: 1 thoi diem ~ xu ly dc 1 command line

		2 CPU
		...
		...

Game:
	- Nhan vat trong game chuyen dong -> Mini App (Thread)
	- nghe nhac -> Thread 2
	- tai du lieu -> Thread 3
	- thao tac UI -> Thread 4
	- ...

Cac van de:
	- Hieu ve Thread la gi?
	- Hieu cach tao Thread & Thread chay nhu the nao
	- Xac dinh thoi diem Thread ton tai
	- Xac dinh xem 1 command line do -> thuoc ve Thread nao?
	- Pause Thread
	- Cach tao Thread???
	- join -> thread

	Main ..............
	T1       .....................
	T2         ...................
	T3        ....................

	Main .......               ......
	T1          ...............
	T2                             ...................
	T3                           .................

#CustomThread.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package java2.lesson04;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author diepvan
 */
public class CustomThread extends Thread{

    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            try {
                System.out.println("T3 > " + i);
                
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
                Logger.getLogger(CustomThread.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    
}

#SharedData.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package java2.lesson04;

/**
 *
 * @author diepvan
 */
public class SharedData {
    int x = 0;
    
    public SharedData() {
        
    }
    
    public synchronized void changeValue(int delta) {
        System.out.println("x = " + x + ", delta = " + delta);
        x += delta;
        System.out.println("x = " + x);
    }
}

#Test01.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package java2.lesson04;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author diepvan
 */
public class Test01 {

    public static void main(String[] args) {
        //Main Thread
        System.out.println("Start app"); //main

        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    System.out.println("T1 > " + i);

                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(Test01.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        });//main
        t1.start();//main + t1
        
        try {
            t1.join();
        } catch (InterruptedException ex) {
            Logger.getLogger(Test01.class.getName()).log(Level.SEVERE, null, ex);
        }

        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    System.out.println("T2 > " + i);

                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(Test01.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        });//main + t1 (co the stop => neu xu ly xong lenh)
        t2.start();//main + t1 + t2

        CustomThread t3 = new CustomThread();
        t3.start();

//        try {
//            Thread.sleep(1000);
//        } catch (InterruptedException ex) {
//            Logger.getLogger(Test01.class.getName()).log(Level.SEVERE, null, ex);
//        }

//        if(t1.isAlive()) {
//            t1.stop();
//        }
//        if(t2.isAlive()) {
//            t2.stop();
//        }

        System.out.println("Stop app");//main + t1 + t2
    }
}

#Test02.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package java2.lesson04;

/**
 *
 * @author diepvan
 */
public class Test02 {
    public static void main(String[] args) {
        SharedData sharedData = new SharedData();
        Thread1 t1 = new Thread1(sharedData);
        Thread2 t2 = new Thread2(sharedData);
        t1.start();
        t2.start();
        
        for (int i = 0; i < 10; i++) {
            sharedData.changeValue(1);
        }
        
        for (int i = 0; i < 10; i++) {
            sharedData.changeValue(1);
        }
    }
}

#Thread1.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package java2.lesson04;

/**
 *
 * @author diepvan
 */
public class Thread1 extends Thread{
    SharedData SharedData;

    public Thread1(SharedData SharedData) {
        this.SharedData = SharedData;
    }
    
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            SharedData.changeValue(1);
        }
    }
    
}

#Thread2.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package java2.lesson04;

/**
 *
 * @author diepvan
 */
public class Thread2 extends Thread{
    SharedData SharedData;

    public Thread2(SharedData SharedData) {
        this.SharedData = SharedData;
    }
    
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            SharedData.changeValue(1);
        }
    }
    
}
Tags:



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

5

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

Đăng nhập để làm bài kiểm tra

Chưa có kết quả nào trước đó