By GokiSoft.com|
19:53 22/05/2020|
Java Advanced
[Share Code] Tìm hiểu Thread trong Java, MultiThreading trong Java, Lập trình Java
[Share Code] Tìm hiểu Thread trong Java, MultiThreading trong Java, Lập trình Java
#Phần 1: Tìm hiểu Thread
##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 lession5;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Diep.Tran
*/
public class Main {
public static void main(String[] args) {
//Main Thread => Luong dau tien
System.out.println("Main Thread Start");
//Khong su dung Thread
// noUsingThread();
//Su dung Thread
//Cach 1
//Khoi tao Thread
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
//bat dau Thread chay
for (int i = 0; i < 10; i++) {
System.out.println("Thread 1 > " + i);
try {
//Khi đặt lệnh Thread.sleep() vào lường nào => thì luồng đó sẽ bị delay.
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
});
//Thread chay
//Khoi tao Thread
Thread1 t2 = new Thread1();
//Thread chay
// Thread2 t3 = new Thread2();
// t3.start();
t1.start();
try {
t1.join();
} catch (InterruptedException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
// t1.stop();
//Thread 1 đã die => không thể start lại được
// t1.start();//câu lẹnh này sẽ bị lỗi
t2.start();
System.out.println("Main Thread Ends");
}
static void noUsingThread() {
for (int i = 0; i < 10; i++) {
System.out.println("Thread 1 > " + i);
}
for (int i = 0; i < 10; i++) {
System.out.println("Thread 2 > " + i);
}
}
}
#Phần 2: Các cách tạo Thread trong Java
##Test.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 lession5;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Diep.Tran
*/
public class Test {
/**
* Tim hieu join() trong Thread
* @param args
*/
public static void main(String[] args) {
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(Test.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
try {
t1.join();
} catch (InterruptedException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
}
for (int i = 0; i < 10; i++) {
System.out.println("T2 > " + i);
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
});
t1.start();
// try {
// t1.join();
// } catch (InterruptedException ex) {
// Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
// }
t2.start();
System.out.println("End Thread Main");
}
}
##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 lession5;
/**
*
* @author Diep.Tran
*/
public class Thread1 extends Thread{
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println("Thread 2 > " + i);
}
}
}
##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 lession5;
/**
*
* @author Diep.Tran
*/
public class Thread2 implements Runnable{
public Thread t;
public Thread2() {
t = new Thread(this);
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println("T3 > " + i);
}
}
public void start() {
t.start();
}
}
#Phần 3: Tìm hiểu đồng bộ Thread trong Java
##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 lession5;
/**
*
* @author Diep.Tran
*/
public class SharedData {
int x = 10;
public SharedData() {
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public synchronized void change(String name, int v) {
System.out.println(name + ", x = " + x + ", v = " + v);
x += v;
System.out.println(name + ", x = " + x);
//vi du: v = 2
//x = 10, v = 2
//x = 12
}
}
##Thread3.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 lession5;
/**
*
* @author Diep.Tran
*/
public class Thread3 extends Thread{
SharedData sharedData;
public Thread3(SharedData sharedData) {
this.sharedData = sharedData;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
sharedData.change("T3", i);
}
}
}
##Thread4.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 lession5;
/**
*
* @author Diep.Tran
*/
public class Thread4 extends Thread{
SharedData sharedData;
public Thread4(SharedData sharedData) {
this.sharedData = sharedData;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
sharedData.change("T4", i);
}
}
}
##Test2.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 lession5;
/**
*
* @author Diep.Tran
*/
public class Test2 {
public static void main(String[] args) {
SharedData sharedData = new SharedData();
//test data dung
sharedData.change("Main", 2);
sharedData.change("Main", 1);
sharedData.change("Main", 3);
Thread3 t3 = new Thread3(sharedData);
Thread4 t4 = new Thread4(sharedData);
t3.start();
t4.start();
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)