• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Question on Thread

 
Ranch Hand
Posts: 128
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Here is the program I ran:



Here is the output it printed:


Removing1: 1
Removing1: 3
Removing1: 5
Removing1: 7
Removing1: 9
Removing2: 10
Removing2: 13
Removing1: 12
Removing1: 16
Removing2: 14
Removing1: 18
Removing2: 19
Removing1: 21
Removing2: 22
Removing1: 24
Removing2: 25
Removing1: 27
Removing2: 28
Removing2: 31
Removing1: 30
Removing2: 32
Removing1: 34
Removing2: 35
Removing1: 37
Removing2: 38
Removing1: 40
Removing2: 41
Removing1: 43
Removing2: 44
Removing1: 46
Removing2: 47
Removing2: 50
Removing1: 49
Removing2: 51
Removing1: 53
Removing2: 54
Removing1: 56
Removing2: 57
Removing1: 59
Removing2: 60
Removing1: 62
Removing2: 63
Removing1: 65
Removing2: 66
Removing1: 68
Removing2: 69
Removing1: 71
Removing2: 72
Removing1: 74
Removing2: 75
Removing1: 77
Removing2: 78
Removing1: 80
Removing2: 81
Removing1: 83
Removing2: 84
Removing1: 86
Removing2: 87
Removing1: 89
Removing2: 90
Removing1: 92
Removing2: 93
Removing1: 95
Removing2: 96
Removing1: 98
Removing2: 99

I see it is missing up lot of numbers in between. Could someone please clarify what is happening?

Thanks, Raghu
 
Marshal
Posts: 4698
588
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first thread is removing two elements, but only printing the value of one of them.
 
Raghavendra Desoju
Ranch Hand
Posts: 128
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yikes, yet another programming mistake
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use Java synchronized method

If you declare any method as synchronized, it is known as synchronized method.

Synchronized method is used to lock an object for any shared resource.

When a thread invokes a synchronized method, it automatically acquires the lock for that object and releases it when the thread completes its task.



Output: 5
10
15
20
25
100
200
300
400
500
 
I am going down to the lab. Do NOT let anyone in. Not even this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic