1

こんにちは、次のコードがあります。

okBtn.addEventListener(Events.ON_CLICK, new EventListener()
{
            @Override
            public void onEvent(final Event arg0) throws Exception
            {
                //when the user clicks on ok, we take the current 
                //string from fckeditor...
                String currentValue = fckEditor.getValue();
                // set the string to preview to the current value 
                html.setContent(currentValue);

            }
 });

私が直面している問題は、この fckEditor.getValue() (fckEditor は textArea に似ています) 呼び出しに遅延があることです。これは、ok アクションが fckEditor.getValue() がデータを取得するために必要なアクションよりも高速であるためです。 fckEditor でテキストをすばやく変更して okBtn を押しても、変更が反映されません。

私はこの解決策を思いついた、

    okBtn.addEventListener(Events.ON_CLICK, new EventListener()
    {
        @Override
        public void onEvent(final Event arg0) throws Exception
        {

            String currentValue;

            synchronized (fckEditor)
            {
                currentValue = fckEditor.getValue();
                fckEditor.wait(100);
            }

            html.setContent(currentValue);

        }

    });

.wait(100);ただし、遅延をハードコーディングしており、コンピューターによって遅延が異なる可能性があるため、これが最適なソリューションであると完全には確信していません。そのため、最終的に他の環境では多少の遅延が必要になる可能性があります。

fckEditor.getValue();呼び出しが完全に終了するまで実行を待機させるにはどうすればよいですか? 正しい文字列を保持してcurrentValue適切に保存できますか?

ありがとうございました

4

2 に答える 2

0
Timer timer = new Timer() {
 public void actionerformed() {
   setRepeats( false );
   String currentValue = fckEditor.getValue();
   try {
      Thread.sleep( 100 );
   } catch( Exception ex ) {
      ex.printStackTrace();
   }//catch
   html.setContent(currentValue);
 }//met
}//inner class
timer.start();
于 2012-04-26T08:20:45.130 に答える
0

パラメーターなしで wait を使用し、エグゼキューターが実行を終了するときに呼び出すnotify必要があります。notifyAll


Marko Topolnik の提案に従って、非常に単純な例を示します。

//declaration
final CountDownLatch latch = new CountDownLatch(1);
...
//in the executor thread
latch.countDown();
//in the waiting thread
exec.start();
latch.await();
于 2012-04-26T08:44:42.333 に答える