Example usage for java.util.concurrent Semaphore acquire

List of usage examples for java.util.concurrent Semaphore acquire

Introduction

In this page you can find the example usage for java.util.concurrent Semaphore acquire.

Prototype

public void acquire() throws InterruptedException 

Source Link

Document

Acquires a permit from this semaphore, blocking until one is available, or the thread is Thread#interrupt interrupted .

Usage

From source file:net.bluehack.ui.ChatActivity.java

@Override
public boolean onFragmentCreate() {
    final int chatId = arguments.getInt("chat_id", 0);
    final int userId = arguments.getInt("user_id", 0);
    final int encId = arguments.getInt("enc_id", 0);
    inlineReturn = arguments.getLong("inline_return", 0);
    String inlineQuery = arguments.getString("inline_query");
    startLoadFromMessageId = arguments.getInt("message_id", 0);
    int migrated_to = arguments.getInt("migrated_to", 0);
    scrollToTopOnResume = arguments.getBoolean("scrollToTopOnResume", false);

    if (chatId != 0) {
        currentChat = MessagesController.getInstance().getChat(chatId);
        if (currentChat == null) {
            final Semaphore semaphore = new Semaphore(0);
            MessagesStorage.getInstance().getStorageQueue().postRunnable(new Runnable() {
                @Override/*from   ww  w . jav a2s  .  c  o  m*/
                public void run() {
                    currentChat = MessagesStorage.getInstance().getChat(chatId);
                    semaphore.release();
                }
            });
            try {
                semaphore.acquire();
            } catch (Exception e) {
                FileLog.e("tmessages", e);
            }
            if (currentChat != null) {
                MessagesController.getInstance().putChat(currentChat, true);
            } else {
                return false;
            }
        }
        if (chatId > 0) {
            dialog_id = -chatId;
        } else {
            isBroadcast = true;
            dialog_id = AndroidUtilities.makeBroadcastId(chatId);
        }
        if (ChatObject.isChannel(currentChat)) {
            MessagesController.getInstance().startShortPoll(chatId, false);
        }
    } else if (userId != 0) {
        currentUser = MessagesController.getInstance().getUser(userId);
        if (currentUser == null) {
            final Semaphore semaphore = new Semaphore(0);
            MessagesStorage.getInstance().getStorageQueue().postRunnable(new Runnable() {
                @Override
                public void run() {
                    currentUser = MessagesStorage.getInstance().getUser(userId);
                    semaphore.release();
                }
            });
            try {
                semaphore.acquire();
            } catch (Exception e) {
                FileLog.e("tmessages", e);
            }
            if (currentUser != null) {
                MessagesController.getInstance().putUser(currentUser, true);
            } else {
                return false;
            }
        }
        dialog_id = userId;
        botUser = arguments.getString("botUser");
        if (inlineQuery != null) {
            MessagesController.getInstance().sendBotStart(currentUser, inlineQuery);
        }
    } else if (encId != 0) {
        currentEncryptedChat = MessagesController.getInstance().getEncryptedChat(encId);
        if (currentEncryptedChat == null) {
            final Semaphore semaphore = new Semaphore(0);
            MessagesStorage.getInstance().getStorageQueue().postRunnable(new Runnable() {
                @Override
                public void run() {
                    currentEncryptedChat = MessagesStorage.getInstance().getEncryptedChat(encId);
                    semaphore.release();
                }
            });
            try {
                semaphore.acquire();
            } catch (Exception e) {
                FileLog.e("tmessages", e);
            }
            if (currentEncryptedChat != null) {
                MessagesController.getInstance().putEncryptedChat(currentEncryptedChat, true);
            } else {
                return false;
            }
        }
        currentUser = MessagesController.getInstance().getUser(currentEncryptedChat.user_id);
        if (currentUser == null) {
            final Semaphore semaphore = new Semaphore(0);
            MessagesStorage.getInstance().getStorageQueue().postRunnable(new Runnable() {
                @Override
                public void run() {
                    currentUser = MessagesStorage.getInstance().getUser(currentEncryptedChat.user_id);
                    semaphore.release();
                }
            });
            try {
                semaphore.acquire();
            } catch (Exception e) {
                FileLog.e("tmessages", e);
            }
            if (currentUser != null) {
                MessagesController.getInstance().putUser(currentUser, true);
            } else {
                return false;
            }
        }
        dialog_id = ((long) encId) << 32;
        maxMessageId[0] = maxMessageId[1] = Integer.MIN_VALUE;
        minMessageId[0] = minMessageId[1] = Integer.MAX_VALUE;
        MediaController.getInstance().startMediaObserver();
    } else {
        return false;
    }

    NotificationCenter.getInstance().addObserver(this, NotificationCenter.messagesDidLoaded);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.emojiDidLoaded);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.updateInterfaces);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.didReceivedNewMessages);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.closeChats);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.messagesRead);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.messagesDeleted);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.messageReceivedByServer);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.messageReceivedByAck);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.messageSendError);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.chatInfoDidLoaded);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.contactsDidLoaded);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.encryptedChatUpdated);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.messagesReadEncrypted);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.removeAllMessagesFromDialog);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.audioProgressDidChanged);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.audioDidReset);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.audioPlayStateChanged);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.screenshotTook);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.blockedUsersDidLoaded);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.FileNewChunkAvailable);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.didCreatedNewDeleteTask);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.audioDidStarted);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.updateMessageMedia);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.replaceMessagesObjects);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.notificationsSettingsUpdated);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.didLoadedReplyMessages);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.didReceivedWebpages);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.didReceivedWebpagesInUpdates);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.messagesReadContent);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.botInfoDidLoaded);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.botKeyboardDidLoaded);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.chatSearchResultsAvailable);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.didUpdatedMessagesViews);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.chatInfoCantLoad);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.didLoadedPinnedMessage);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.peerSettingsDidLoaded);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.newDraftReceived);

    super.onFragmentCreate();

    if (currentEncryptedChat == null && !isBroadcast) {
        BotQuery.loadBotKeyboard(dialog_id);
    }

    loading = true;
    MessagesController.getInstance().loadPeerSettings(dialog_id, currentUser, currentChat);
    MessagesController.getInstance().setLastCreatedDialogId(dialog_id, true);
    if (startLoadFromMessageId != 0) {
        needSelectFromMessageId = true;
        waitingForLoad.add(lastLoadIndex);
        if (migrated_to != 0) {
            mergeDialogId = migrated_to;
            MessagesController.getInstance().loadMessages(mergeDialogId, AndroidUtilities.isTablet() ? 30 : 20,
                    startLoadFromMessageId, true, 0, classGuid, 3, 0, ChatObject.isChannel(currentChat),
                    lastLoadIndex++);
        } else {
            MessagesController.getInstance().loadMessages(dialog_id, AndroidUtilities.isTablet() ? 30 : 20,
                    startLoadFromMessageId, true, 0, classGuid, 3, 0, ChatObject.isChannel(currentChat),
                    lastLoadIndex++);
        }
    } else {
        waitingForLoad.add(lastLoadIndex);
        MessagesController.getInstance().loadMessages(dialog_id, AndroidUtilities.isTablet() ? 30 : 20, 0, true,
                0, classGuid, 2, 0, ChatObject.isChannel(currentChat), lastLoadIndex++);
    }

    if (currentChat != null) {
        Semaphore semaphore = null;
        if (isBroadcast) {
            semaphore = new Semaphore(0);
        }
        MessagesController.getInstance().loadChatInfo(currentChat.id, semaphore,
                ChatObject.isChannel(currentChat));
        if (isBroadcast && semaphore != null) {
            try {
                semaphore.acquire();
            } catch (Exception e) {
                FileLog.e("tmessages", e);
            }
        }
    }

    if (userId != 0 && currentUser.bot) {
        BotQuery.loadBotInfo(userId, true, classGuid);
    } else if (info instanceof TLRPC.TL_chatFull) {
        for (int a = 0; a < info.participants.participants.size(); a++) {
            TLRPC.ChatParticipant participant = info.participants.participants.get(a);
            TLRPC.User user = MessagesController.getInstance().getUser(participant.user_id);
            if (user != null && user.bot) {
                BotQuery.loadBotInfo(user.id, true, classGuid);
            }
        }
    }

    if (currentUser != null) {
        userBlocked = MessagesController.getInstance().blockedUsers.contains(currentUser.id);
    }

    if (AndroidUtilities.isTablet()) {
        NotificationCenter.getInstance().postNotificationName(NotificationCenter.openedChatChanged, dialog_id,
                false);
    }

    if (currentEncryptedChat != null && AndroidUtilities
            .getMyLayerVersion(currentEncryptedChat.layer) != SecretChatHelper.CURRENT_SECRET_CHAT_LAYER) {
        SecretChatHelper.getInstance().sendNotifyLayerMessage(currentEncryptedChat, null);
    }

    return true;
}

From source file:kr.wdream.ui.ChatActivity.java

@Override
public boolean onFragmentCreate() {
    final int chatId = arguments.getInt("chat_id", 0);
    final int userId = arguments.getInt("user_id", 0);
    final int encId = arguments.getInt("enc_id", 0);
    inlineReturn = arguments.getLong("inline_return", 0);
    String inlineQuery = arguments.getString("inline_query");
    startLoadFromMessageId = arguments.getInt("message_id", 0);
    int migrated_to = arguments.getInt("migrated_to", 0);
    scrollToTopOnResume = arguments.getBoolean("scrollToTopOnResume", false);

    if (chatId != 0) {
        currentChat = MessagesController.getInstance().getChat(chatId);
        if (currentChat == null) {
            final Semaphore semaphore = new Semaphore(0);
            MessagesStorage.getInstance().getStorageQueue().postRunnable(new Runnable() {
                @Override//from   ww w.  j  a v  a 2s .  c  o  m
                public void run() {
                    currentChat = MessagesStorage.getInstance().getChat(chatId);
                    semaphore.release();

                    Log.d(LOG_TAG, "currentChat : " + currentChat);
                }
            });
            try {
                semaphore.acquire();
            } catch (Exception e) {
                FileLog.e("tmessages", e);
            }
            if (currentChat != null) {
                MessagesController.getInstance().putChat(currentChat, true);

                Log.d(LOG_TAG, "currentChat2 : " + currentChat);
            } else {
                return false;
            }
        }
        if (chatId > 0) {
            dialog_id = -chatId;
        } else {
            isBroadcast = true;
            dialog_id = AndroidUtilities.makeBroadcastId(chatId);
        }
        if (ChatObject.isChannel(currentChat)) {
            MessagesController.getInstance().startShortPoll(chatId, false);
        }
    } else if (userId != 0) {
        currentUser = MessagesController.getInstance().getUser(userId);
        if (currentUser == null) {
            final Semaphore semaphore = new Semaphore(0);
            MessagesStorage.getInstance().getStorageQueue().postRunnable(new Runnable() {
                @Override
                public void run() {
                    currentUser = MessagesStorage.getInstance().getUser(userId);
                    semaphore.release();
                }
            });
            try {
                semaphore.acquire();
            } catch (Exception e) {
                FileLog.e("tmessages", e);
            }
            if (currentUser != null) {
                MessagesController.getInstance().putUser(currentUser, true);
            } else {
                return false;
            }
        }
        dialog_id = userId;
        botUser = arguments.getString("botUser");
        if (inlineQuery != null) {
            MessagesController.getInstance().sendBotStart(currentUser, inlineQuery);
        }
    } else if (encId != 0) {
        currentEncryptedChat = MessagesController.getInstance().getEncryptedChat(encId);
        if (currentEncryptedChat == null) {
            final Semaphore semaphore = new Semaphore(0);
            MessagesStorage.getInstance().getStorageQueue().postRunnable(new Runnable() {
                @Override
                public void run() {
                    currentEncryptedChat = MessagesStorage.getInstance().getEncryptedChat(encId);
                    semaphore.release();
                }
            });
            try {
                semaphore.acquire();
            } catch (Exception e) {
                FileLog.e("tmessages", e);
            }
            if (currentEncryptedChat != null) {
                MessagesController.getInstance().putEncryptedChat(currentEncryptedChat, true);
            } else {
                return false;
            }
        }
        currentUser = MessagesController.getInstance().getUser(currentEncryptedChat.user_id);
        if (currentUser == null) {
            final Semaphore semaphore = new Semaphore(0);
            MessagesStorage.getInstance().getStorageQueue().postRunnable(new Runnable() {
                @Override
                public void run() {
                    currentUser = MessagesStorage.getInstance().getUser(currentEncryptedChat.user_id);
                    semaphore.release();
                }
            });
            try {
                semaphore.acquire();
            } catch (Exception e) {
                FileLog.e("tmessages", e);
            }
            if (currentUser != null) {
                MessagesController.getInstance().putUser(currentUser, true);
            } else {
                return false;
            }
        }
        dialog_id = ((long) encId) << 32;
        maxMessageId[0] = maxMessageId[1] = Integer.MIN_VALUE;
        minMessageId[0] = minMessageId[1] = Integer.MAX_VALUE;
        MediaController.getInstance().startMediaObserver();
    } else {
        return false;
    }

    NotificationCenter.getInstance().addObserver(this, NotificationCenter.messagesDidLoaded);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.emojiDidLoaded);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.updateInterfaces);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.didReceivedNewMessages);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.closeChats);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.messagesRead);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.messagesDeleted);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.messageReceivedByServer);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.messageReceivedByAck);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.messageSendError);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.chatInfoDidLoaded);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.contactsDidLoaded);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.encryptedChatUpdated);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.messagesReadEncrypted);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.removeAllMessagesFromDialog);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.audioProgressDidChanged);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.audioDidReset);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.audioPlayStateChanged);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.screenshotTook);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.blockedUsersDidLoaded);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.FileNewChunkAvailable);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.didCreatedNewDeleteTask);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.audioDidStarted);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.updateMessageMedia);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.replaceMessagesObjects);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.notificationsSettingsUpdated);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.didLoadedReplyMessages);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.didReceivedWebpages);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.didReceivedWebpagesInUpdates);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.messagesReadContent);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.botInfoDidLoaded);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.botKeyboardDidLoaded);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.chatSearchResultsAvailable);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.didUpdatedMessagesViews);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.chatInfoCantLoad);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.didLoadedPinnedMessage);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.peerSettingsDidLoaded);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.newDraftReceived);

    super.onFragmentCreate();

    if (currentEncryptedChat == null && !isBroadcast) {
        BotQuery.loadBotKeyboard(dialog_id);
    }

    loading = true;
    MessagesController.getInstance().loadPeerSettings(dialog_id, currentUser, currentChat);
    MessagesController.getInstance().setLastCreatedDialogId(dialog_id, true);
    if (startLoadFromMessageId != 0) {
        needSelectFromMessageId = true;
        waitingForLoad.add(lastLoadIndex);
        if (migrated_to != 0) {
            mergeDialogId = migrated_to;
            MessagesController.getInstance().loadMessages(mergeDialogId, AndroidUtilities.isTablet() ? 30 : 20,
                    startLoadFromMessageId, true, 0, classGuid, 3, 0, ChatObject.isChannel(currentChat),
                    lastLoadIndex++);
        } else {
            MessagesController.getInstance().loadMessages(dialog_id, AndroidUtilities.isTablet() ? 30 : 20,
                    startLoadFromMessageId, true, 0, classGuid, 3, 0, ChatObject.isChannel(currentChat),
                    lastLoadIndex++);
        }
    } else {
        waitingForLoad.add(lastLoadIndex);
        MessagesController.getInstance().loadMessages(dialog_id, AndroidUtilities.isTablet() ? 30 : 20, 0, true,
                0, classGuid, 2, 0, ChatObject.isChannel(currentChat), lastLoadIndex++);
    }

    if (currentChat != null) {
        Semaphore semaphore = null;
        if (isBroadcast) {
            semaphore = new Semaphore(0);
        }
        MessagesController.getInstance().loadChatInfo(currentChat.id, semaphore,
                ChatObject.isChannel(currentChat));
        if (isBroadcast && semaphore != null) {
            try {
                semaphore.acquire();
            } catch (Exception e) {
                FileLog.e("tmessages", e);
            }
        }
    }

    if (userId != 0 && currentUser.bot) {
        BotQuery.loadBotInfo(userId, true, classGuid);
    } else if (info instanceof TLRPC.TL_chatFull) {
        for (int a = 0; a < info.participants.participants.size(); a++) {
            TLRPC.ChatParticipant participant = info.participants.participants.get(a);
            TLRPC.User user = MessagesController.getInstance().getUser(participant.user_id);
            if (user != null && user.bot) {
                BotQuery.loadBotInfo(user.id, true, classGuid);
            }
        }
    }

    if (currentUser != null) {
        userBlocked = MessagesController.getInstance().blockedUsers.contains(currentUser.id);
    }

    if (AndroidUtilities.isTablet()) {
        NotificationCenter.getInstance().postNotificationName(NotificationCenter.openedChatChanged, dialog_id,
                false);
    }

    if (currentEncryptedChat != null && AndroidUtilities
            .getMyLayerVersion(currentEncryptedChat.layer) != SecretChatHelper.CURRENT_SECRET_CHAT_LAYER) {
        SecretChatHelper.getInstance().sendNotifyLayerMessage(currentEncryptedChat, null);
    }

    return true;
}