| |

VerySource

 Forgot password?
 Register
Search
View: 3185|Reply: 19

An interesting topic: about multithreading! !!

[Copy link]

2

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-1-6 19:10:01
| Show all posts |Read mode
I wrote this applet myself today

The requirements for this topic are:
There are 4 pairs of families: father, mother, son, daughter

Dads are responsible for putting apples on the plate, and moms are responsible for putting oranges on the plate.
Sons specialize in oranges on plates; daughters specialize in apples on plates;

Note; there can be no more than 10 fruits on the plate, and only one fruit can be placed or taken at a time;

That's all, the code is below, and there are some problems in running. It seems that sometimes some methods are accessed multiple times.
If you are interested, you can run the code and help me analyze the reason, thank you
class ThreadTest
{
Ransom
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);
Ranch
Mother m1 = new Mother (q);
Mother m2 = new Mother (q);
Mother m3 = new Mother (q);
Mother m4 = new Mother (q);
Ranch
Son s1 = new Son (q);
Son s2 = new Son (q);
Son s3 = new Son (q);
Son s4 = new Son (q);
Ranch
Daughter d1 = new Daughter (q);
Daughter d2 = new Daughter (q);
Daughter d3 = new Daughter (q);
Daughter d4 = new Daughter (q);
Ranch
f1.start ();
f2.start ();
f3.start ();
f4.start ();
Ranch
m1.start ();
m2.start ();
m3.start ();
m4.start ();
Ranch
s1.start ();
s2.start ();
s3.start ();
s4.start ();
Ranch
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)
{
q.putApple (q.apple);
System.out.println (Thread.currentThread (). GetName () + "put one apple:

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

orange: the number of orange in the plate: "+ q.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
{
Ransom
int apple = 0;
int orange = 0;
int pFull = 10;
public synchronized void putApple (int i)
{
if (pFull <11&&pFull> 0)
{
i ++;
apple = i;
pFull--;
notifyAll ();
}
try
{
wait ();
}
catch (Exception e)
{
e.printStackTrace ();
}
Ranch
Ranch
}
public synchronized int getApple ()
{
if (apple <1 || apple> 10)
{
try
{
wait ();
}
catch (Exception e)
{
e.printStackTrace ();
}
Ranch
}
pFull ++;
Ranch
notifyAll ();
apple--;
return apple;
}
public synchronized void putOrange (int i)
{
if (pFull <11&&pFull> 0)
{
i ++;
orange = i;
pFull--;
notifyAll ();
}
try
{
wait ();
}
catch (Exception e)
{
e.printStackTrace ();
}
Ranch
Ranch
}
public synchronized int getOrange ()
{
while (orange <1 || orange> 11)
{
try
{
wait ();
}
catch (Exception e)
{
e.printStackTrace ();
}
}
pFull ++;
Ranch
notifyAll ();
orange--;
return orange;
}
}
Reply

Use magic Report

1

Threads

51

Posts

32.00

Credits

Newbie

Rank: 1

Credits
32.00

 China

Post time: 2020-1-7 13:39:01
| Show all posts
UP down
Reply

Use magic Report

0

Threads

1

Posts

1.00

Credits

Newbie

Rank: 1

Credits
1.00

 China

Post time: 2020-1-7 21:49:55
| Show all posts
UP down
Reply

Use magic Report

2

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

 Author| Post time: 2020-1-9 19:09:01
| Show all posts
Can anyone help explain the following?
Reply

Use magic Report

0

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-1-10 05:36:01
| Show all posts
mark
Reply

Use magic Report

0

Threads

6

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-10 21:00:01
| Show all posts
up
Reply

Use magic Report

0

Threads

6

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-11 01:09:01
| Show all posts
Although your get and set methods are synchronous, the two get and two set methods can be performed at the same time, resulting in a pfull of -1 and eventually all threads deadlock.
Reply

Use magic Report

0

Threads

1

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-1-11 09:01:47
| Show all posts
Very good, learn! !! !!
Reply

Use magic Report

2

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

 Author| Post time: 2020-1-13 17:54:01
| Show all posts
How do I need to change it to get the two get methods or set methods?

It's forbidden to do both

Suggestions, thank you
Reply

Use magic Report

0

Threads

8

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2020-1-16 14:27:01
| Show all posts
Use a Vector that stores orange or apple objects (when size () <10), synchronized when using Vector
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