• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Question on Thread

 
Ranch Hand
Posts: 127
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
 
Sheriff
Posts: 4006
549
Android 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: 127
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 have always wanted to have a neighbor just like you - Fred Rogers. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic