@@ -92,32 +92,43 @@ public void handleSignal(ShutdownSignalException signal) {
9292 }
9393 }
9494
95- public synchronized ChannelN createChannel (AMQConnection connection ) throws IOException {
96- int channelNumber = channelNumberAllocator .allocate ();
97- if (channelNumber == -1 ) {
98- return null ;
95+ public ChannelN createChannel (AMQConnection connection ) throws IOException {
96+ int channelNumber ;
97+ synchronized (this ) {
98+ channelNumber = channelNumberAllocator .allocate ();
99+ if (channelNumber == -1 ) {
100+ return null ;
101+ }
99102 }
100103 return createChannelInternal (connection , channelNumber );
101104 }
102105
103- public synchronized ChannelN createChannel (AMQConnection connection , int channelNumber ) throws IOException {
104- if (channelNumberAllocator .reserve (channelNumber ))
106+ public ChannelN createChannel (AMQConnection connection , int channelNumber ) throws IOException {
107+ boolean reserved ;
108+ synchronized (this ) {
109+ reserved = channelNumberAllocator .reserve (channelNumber );
110+ }
111+ if (reserved )
105112 return createChannelInternal (connection , channelNumber );
106113 else
107114 return null ;
108115 }
109116
110- private synchronized ChannelN createChannelInternal (AMQConnection connection , int channelNumber ) throws IOException {
111- if (_channelMap .containsKey (channelNumber )) {
112- // That number's already allocated! Can't do it
113- // This should never happen unless something has gone
114- // badly wrong with our implementation.
115- throw new IllegalStateException ("We have attempted to "
116- + "create a channel with a number that is already in "
117- + "use. This should never happen. Please report this as a bug." );
117+ private ChannelN createChannelInternal (AMQConnection connection , int channelNumber ) throws IOException {
118+ ChannelN ch ;
119+ synchronized (this ) {
120+ if (_channelMap .containsKey (channelNumber )) {
121+ // That number's already allocated! Can't do it
122+ // This should never happen unless something has gone
123+ // badly wrong with our implementation.
124+ throw new IllegalStateException ("We have attempted to "
125+ + "create a channel with a number that is already in "
126+ + "use. This should never happen. "
127+ + "Please report this as a bug." );
128+ }
129+ ch = new ChannelN (connection , channelNumber );
130+ addChannel (ch );
118131 }
119- ChannelN ch = new ChannelN (connection , channelNumber );
120- addChannel (ch );
121132 ch .open (); // now that it's been added to our internal tables
122133 return ch ;
123134 }
0 commit comments