Java multi-threading 2.2. Inter-thread communication

Inter-thread communication problem

1, inter-thread communication problem

(1) Inter-thread communication problem: operation of the same resource between different kinds of threads.

2) operates on the same student object by setting the thread (producer) and the get thread (consumer).

2, inter-thread communication example

(1)Create student class

package cn.itcast_03;
public class Student {
	String name;
	int age;
}

(2)Create producer

package cn.itcast_03;
Public class SetThread implements Runnable {
Private Student s;
Public SetThread(Student s) {
This.s = s;
}
@Override
Public void run() {
S.name = "Lin Qingxia";
S.age = 27;
}
}

(3)Create consumer

package cn.itcast_03;
public class GetThread implements Runnable {
	private Student s;
	public GetThread(Student s) {
		this.s = s;
	}
	@Override
	public void run() {
		System.out.println(s.name + "---" + s.age);
	}
}

(4)Test thread class: This method has a problem: need to lock to solve

package cn.itcast_03;

/*
 * Analysis:
 * Resource class: Student
 * Set student data: SetThread (producer)
 * Get student data: GetThread (consumer)
 * Test class: StudentDemo
 *
 * Question 1: Write the code according to the idea and find that the data is: null---0
 * Reason: We created new resources in each thread, and the resources we set and get for the thread should be the same when we requested it.
 * How to achieve it?
 * Create this data in the outside world and pass it to other classes through the constructor.
 *
 */
Public class StudentDemo {
Public static void main(String[] args) {
/ / Create a resource
Student s = new Student();
/ / Set and get the class
SetThread st = new SetThread(s);
GetThread gt = new GetThread(s);
//Thread class
Thread t1 = new Thread(st);
Thread t2 = new Thread(gt);
/ / Start the thread
T1.start();
T2.start();
}
}

3, Inter-thread communication example problem solving

(1)Create student class

package cn.itcast_03;
public class Student {
	String name;
	int age;
}

(2)Create producer

package cn.itcast_04;
Public class SetThread implements Runnable {
Private Student s;
Private int x = 0;
Public SetThread(Student s) {
This.s = s;
}
@Override
Public void run() {
While (true) {
Synchronized (s) {
If (x % 2 == 0) {
S.name = "Lin Qingxia"; / / just got here, they were grabbed by others
S.age = 27;
} else {
S.name = "Liu Yi"; //When I got here, I was robbed of execution rights by others.
S.age = 30;
}
x++;
}
}
}
}

(3)Create a consumer

package cn.itcast_04;
public class GetThread implements Runnable {
	private Student s;
	public GetThread(Student s) {
		this.s = s;
	}
	@Override
	public void run() {
		while (true) {
			synchronized (s) {
				System.out.println(s.name + "---" + s.age);
			}
		}
	}
}

(4)Test thread class

package cn.itcast_04;

Public class StudentDemo {
Public static void main(String[] args) {
/ / Create a resource
Student s = new Student();

/ / Set and get the class
SetThread st = new SetThread(s);
GetThread gt = new GetThread(s);

//Thread class
Thread t1 = new Thread(st);
Thread t2 = new Thread(gt);

/ / Start the thread
T1.start();
T2.start();
}
}

* Analysis:* Resource Class: Student * Set student data: SetThread (producer) * Get student data: GetThread (consumer) * Test class: StudentDemo * * Question 1: Write the code according to the idea and find that the data is: null---0 * Reason: We created new resources in each thread, and the resources we set and get the thread when we requested should be the same! * How to achieve it? * Create this data in the outside world and pass it to other classes through the constructor. * Question 2: In order to make the data work better, I added loops and judgments, giving different values, this time a new problem * A: The same data appears multiple times * B: Name and age do not match * the reason: * A: The same data appears multiple times * The CPU's execution time for a little bit of time is enough for you to execute many times. * B: Name and age do not match * Randomness of thread running * Thread safety issues: * A: Is it a multi-threaded environment? * B: Is there any shared data? * C: Is there a number of statement operations to share data? * Solution:  *        加锁. * Note: * A: Different kinds of threads must be locked. * B: Different types of threads must have the same lock.

4、