By GokiSoft.com|
10:34 18/08/2021|
Java Advanced
[Share Code] Tìm hiểu về Thread - Đồng bộ Thread synchonized 1 function và trên nhiều functions - C2010G
Phan 1:
#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 java2.lesson05;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Main Thread
*
* @author Diep.Tran
*/
public class Main {
public static void main(String[] args) {
System.out.println("Main Thread start ...");
System.out.println("step 1");
//Khoi tao luong moi. T1
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
// Scanner scan = new Scanner(System.in);
//Bat dau xu ly code cua luong moi.
for (int i = 0; i < 30; i++) {
// System.out.println("T1 -> enter -> ");
// int v1 = scan.nextInt();
System.out.println("T1 -> " + i);
}
}
});
//Khoi tao luong T2
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
// Scanner scan = new Scanner(System.in);
//T2 -> xu ly khoi code trong T2
for (int i = 0; i < 30; i++) {
System.out.println("T2 -> enter -> ");
// int v2 = scan.nextInt();
System.out.println("T2 -> " + 2);
}
}
});
t1.start();//Bat dau chay luong T1
try {
t1.join();
} catch (InterruptedException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
t2.start();//T2 bat dau chay
System.out.println("step 2");
System.out.println("Main Thread stop");
}
}
Phan 2:#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 java2.lesson05;
/**
*
* @author Diep.Tran
*/
public class Test {
public static void main(String[] args) {
//Khoi tao Thread : Tong cong 3 thread -> Main, T1, T2
//Ton tai duy nhat Main thread
System.out.println("Main -> step -> 1");//Main
CustomThread t1 = new CustomThread();//Main
System.out.println("Main -> step -> 2");//Main
t1.setName("T1");//Main
System.out.println("Main -> step -> 3");//Main
CustomThread t2 = new CustomThread();//Main
System.out.println("Main -> step -> 4");//Main
t2.setName("T2");//Main
System.out.println("Main -> step -> 5");//Main
//Bat thread chay
t2.start();//Main + T2 bat dau chay
System.out.println("Main -> step -> 6");//Main + T2 (Ton tai T2)
t1.start();//Main + T2 (Ton tai T2) + T1 (Bat dau chay)
System.out.println("Main -> step -> 7");//Main + T2 (Co The Ton tai T2) + T1 (Co The Ton tai T1)
for (int i = 0; i < 10; i++) {
System.out.println("Main -> " + i);//Main + T2 (Co The Ton tai T2) + T1 (Co The Ton tai T1)
}
System.out.println("Stop");//Main + T2 (Co The Ton tai T2) + T1 (Co The Ton tai T1)
}
}
#CustomThread.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 java2.lesson05;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Diep.Tran
*/
public class CustomThread extends Thread{
@Override
public void run() {
System.out.println("Bat dau xu ly 1 luong moi -> " + getName());
for (int i = 0; i < 20; i++) {
try {
Thread.sleep(1000);
System.out.println(getName() + " -> " + i);
} catch (InterruptedException ex) {
Logger.getLogger(CustomThread.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
Phan 3:
#TestThread02.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 java2.lesson05;
/**
*
* @author Diep.Tran
*/
public class TestThread02 extends Thread {
boolean isAdd = false;
SharedData sharedData;
public TestThread02(SharedData sharedData, boolean isAdd) {
this.isAdd = isAdd;
this.sharedData = sharedData;
}
@Override
public void run() {
for (int i = 0; i < 30; i++) {
if (isAdd) {
synchronized (sharedData) {
sharedData.plus(1);
}
} else {
synchronized (sharedData) {
sharedData.minus(1);
}
}
}
}
}
#MultiThreadTest01.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 java2.lesson05;
/**
*
* @author Diep.Tran
*/
public class MultiThreadTest01 {
static class TestThread01 extends Thread {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
addMore(1);
}
}
}
public static void main(String[] args) {
SharedData sharedData = new SharedData();
TestThread02 t1 = new TestThread02(sharedData, true);
TestThread02 t2 = new TestThread02(sharedData, true);
TestThread02 t3 = new TestThread02(sharedData, false);
TestThread02 t4 = new TestThread02(sharedData, false);
TestThread02 t5 = new TestThread02(sharedData, false);
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
}
/**
* Thuc hien kiem tra da luong tren 1 function
*/
static void testSync01() {
TestThread01 t1 = new TestThread01();
TestThread01 t2 = new TestThread01();
TestThread01 t3 = new TestThread01();
t1.start();
t2.start();
t3.start();
}
static int count = 0;
static synchronized void addMore(int delta) {
System.out.println("Before: " + count + ", delta: " + delta);
count+=delta;
System.out.println("After: " + count);
}
}
#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 java2.lesson05;
/**
*
* @author Diep.Tran
*/
public class SharedData {
int count = 0;
public synchronized void addMore(int delta, boolean isAdd) {
if(isAdd) {
plus(delta);
} else {
minus(delta);
}
}
public void plus(int delta) {
System.out.println("Before: " + count + ", delta: " + delta);
count+=delta;
System.out.println("After: " + count);
}
public void minus(int delta) {
System.out.println("~~~Before: " + count + ", delta: " + delta);
count-=delta;
System.out.println("~~~After: " + count);
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)