| |

VerySource

 Forgot password?
 Register
Search
Author: kippakiss

An interesting topic: about multithreading! !!

[Copy link]

2

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

 Author| Post time: 2020-1-17 14:45:02
| Show all posts
Can I change it based on my source code?

Very grateful! !!

I've been stuck on this problem for several days
Reply

Use magic Report

0

Threads

5

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-1-18 11:27:01
| Show all posts
Because the plates will have shared mutual exclusion,
So you should focus your attention on it.
Reply

Use magic Report

2

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 India

 Author| Post time: 2020-1-20 21:09:01
| Show all posts
Can you tap some code and demonstrate

Take the example of only two families f1, m1, s1, d1
                        f2, m2, s2, d2

I'm getting more confused now.
Reply

Use magic Report

0

Threads

4

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-21 16:45:54
| Show all posts
add some code into your "get (), set ()"
static Vector vector = new Vector (10); // class variable
such as:
synchronized (vector.size () ...) {
..... // do something with your
}
Reply

Use magic Report

0

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-1-21 20:18:01
| Show all posts
q.putOrange (q.orange);
Can't guarantee the synchronization of q.orange?

Can public synchronized void putOrange (int i) be changed to the following?
public synchronized int putOrange ()

The return value is q.orange.
Reply

Use magic Report

0

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-1-30 05:36:01
| Show all posts
package test;

public class ThreadTest {

public static void main (String [] args) {
Plate q = new Plate ();
Father f1 = new Father (q);
Father f2 = new Father (q);
Father f3 = new Father (q);
Father f4 = new Father (q);

Mother m1 = new Mother (q);
Mother m2 = new Mother (q);
Mother m3 = new Mother (q);
Mother m4 = new Mother (q);

Son s1 = new Son (q);
Son s2 = new Son (q);
Son s3 = new Son (q);
Son s4 = new Son (q);

Daughter d1 = new Daughter (q);
Daughter d2 = new Daughter (q);
Daughter d3 = new Daughter (q);
Daughter d4 = new Daughter (q);

f1.start ();
f2.start ();
f3.start ();
f4.start ();

m1.start ();
m2.start ();
m3.start ();
m4.start ();

s1.start ();
s2.start ();
s3.start ();
s4.start ();

d1.start ();
d2.start ();
d3.start ();
d4.start ();
}
}

class Father extends Thread {
Plate q;

Father (Plate q) {
this.q = q;
}

public void run () {
while (true) {
int apple = q.putApple ();
System.out.println (Thread.currentThread (). GetName ()
+ "put one apple: the number of apple in the plate:"
+ apple);
}
}
}

class Mother extends Thread {
Plate q;

Mother (Plate q) {
this.q = q;
}

public void run () {
while (true) {
int orange = q.putOrange ();
System.out.println (Thread.currentThread (). GetName ()
+ "put one orange: the number of orange in the plate:"
+ orange);
}
}
}

class Son extends Thread {
Plate q;

Son (Plate q) {
this.q = q;
}

public void run () {
while (true) {
System.out.println (Thread.currentThread (). GetName ()
+ "get one orange: the number of orange in the plate:"
+ q.getOrange ());
}
}
}

class Daughter extends Thread {
Plate q;

Daughter (Plate q) {
this.q = q;
}

public void run () {
while (true) {
System.out
.println (Thread.currentThread (). getName ()
+ "Daughter get one apple: the number of apple in the plate:"
+ q.getApple ());
}
}
}

class Plate {

private int apple = 0;
private int orange = 0;
private int pFull = 10;

public synchronized int putApple () {

while (! Thread.currentThread (). isInterrupted ()) {
if (pFull> 0) {
apple ++;
pFull--;
break;
} else {
try {
wait ();
} catch (Exception e) {
e.printStackTrace ();
}
}
}
notifyAll ();
return apple;
}

public synchronized int getApple () {
while (apple <= 0) {
try {
wait ();
} catch (Exception e) {
e.printStackTrace ();
}
}
pFull ++;
apple--;
notifyAll ();
return apple + 1;
}

public synchronized int putOrange () {
while (! Thread.currentThread (). isInterrupted ()) {
if (pFull> 0) {
orange ++;
pFull--;
break;
} else {
try {
wait ();
} catch (Exception e) {
e.printStackTrace ();
}
}
}
notifyAll ();
return orange;
}

public synchronized int getOrange () {
while (orange <= 0) {
try {
wait ();
} catch (Exception e) {
e.printStackTrace ();
}
}
pFull ++;
orange--;
notifyAll ();
return orange + 1;
}
}
Reply

Use magic Report

0

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 Asia/Pacific Region

Post time: 2020-5-6 19:30:01
| Show all posts
How can this control synchronization?
When there is nothing at first, two kinds (8) of get threads are blocked, and two kinds (8) of put threads compete. Assuming that one of the putApple wins, it will put 1 apple and wake up the other threads to continue the competition. If a getOrange wins at this time, it will be executed from the wait block, so the program will take an orange. . . Obvious logical errors.

Method:
Define two lists (array or Integer is also possible) on the plate, one orange and one apple, and use these two things to control the two pairs of get / put methods respectively.
Reply

Use magic Report

0

Threads

11

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-5-24 00:30:01
| Show all posts
Operating system Examples in class ...
Reply

Use magic Report

0

Threads

1

Posts

0.00

Credits

Newbie

Rank: 1

Credits
0.00

 China

Post time: 2020-5-24 17:20:38
| Show all posts
I want to mix points under up
Reply

Use magic Report

1

Threads

6

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-6-21 20:00:01
| Show all posts
Try it out, it doesn't necessarily meet your requirements!
public class ThreadTest
{
public static void main(String[] args)
{
Fruits q = new Fruits();
The
Father f1=new Father(q);
Father f2=new Father(q);
Father f3=new Father(q);
Father f4=new Father(q);
The
Mother m1=new Mother(q);
Mother m2=new Mother(q);
Mother m3=new Mother(q);
Mother m4=new Mother(q);
The
Son s1=new Son(q);
Son s2=new Son(q);
Son s3=new Son(q);
Son s4=new Son(q);
The
Daughter d1=new Daughter(q);
Daughter d2=new Daughter(q);
Daughter d3=new Daughter(q);
Daughter d4=new Daughter(q);
The
f1.start();
f2.start();
f3.start();
f4.start();
The
m1.start();
m2.start();
m3.start();
m4.start();
The
s1.start();
s2.start();
s3.start();
s4.start();
The
d1.start();
d2.start();
d3.start();
d4.start();
}
}

class Father extends Thread
{
Fruits fruit;
Father(Fruits fruit)
{
this.fruit = fruit;
}
The
public void run()
{
while(true)
{
fruit.setApple();
System.out.println(Thread.currentThread().getName() +
"put one apple: the number of apple in the plate:" + fruit.appleNumber +
"the number of full in the plate:" + fruit.full);
}
}
}

class Mother extends Thread
{
Fruits fruit;
Mother(Fruits fruit)
{
this.fruit = fruit;
}
The
public void run()
{
while(true)
{
fruit.setOrange();
System.out.println(Thread.currentThread().getName() +
"put one oranger: the number of orange in the plate:" + fruit.orangeNumber +
"the number of full in the plate:" + fruit.full);
}
}
}

class Son extends Thread
{
Fruits fruit;
Son(Fruits fruit)
{
this.fruit = fruit;
}
The
public void run()
{
while(true)
{
fruit.getOrange();
System.out.println(Thread.currentThread().getName() +
"get one orange: the number of orange in the plate:" + fruit.orangeNumber +
"the number of full in the plate:" + fruit.full);
}
}
}

class Daughter extends Thread
{
Fruits fruit;
Daughter(Fruits fruit)
{
this.fruit = fruit;
}
The
public void run()
{
while(true)
{
fruit.getApple();
System.out.println(Thread.currentThread().getName() +
"get one apple: the number of apple in the plate:" + fruit.appleNumber +
"the number of full in the plate:" + fruit.full);
}
}
}

class Fruits
{
int appleNumber = 0;
int orangeNumber = 0;
int full = 0;
The
public synchronized int setApple()
{
if(full <10&&full >= 0&&appleNumber <10)
{
appleNumber += 1;
full++;
}
return appleNumber;
}
The
public synchronized int getApple()
{
if(full >= 1&&full <=10&&appleNumber >= 1)
{
appleNumber -= 1;
full--;
}
return appleNumber;
}
The
public synchronized int setOrange()
{
if(full <10&&full >= 0&&orangeNumber <10)
{
orangeNumber += 1;
full++;
}
return orangeNumber;
}
The
public synchronized int getOrange()
{
if(full >= 1&&full <=10&&orangeNumber >= 1)
{
orangeNumber -= 1;
full--;
}
return orangeNumber;
}
The
public synchronized int getFull()
{
return appleNumber + orangeNumber;
}
}
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list