Search in sources :

Example 76 with TimerTask

use of java.util.TimerTask in project mastering-java by Kingminghuang.

the class HttpClientCrawler method testHardTimeout.

private static void testHardTimeout() throws IOException {
    int hardTimeout = 15;
    RequestConfig requestConfig = RequestConfig.custom().setProxy(new HttpHost(DEFAULT_PROXY, DEFAULT_PORT, DEFAULT_SCHEMA)).build();
    config(requestConfig);
    HttpGet httpGet = new HttpGet(EXAMPLE_URL);
    TimerTask task = new TimerTask() {

        @Override
        public void run() {
            if (httpGet != null) {
                httpGet.abort();
            }
        }
    };
    new Timer(true).schedule(task, hardTimeout * 1000);
    HttpResponse response = client.execute(httpGet);
    System.out.println(response.getStatusLine().getStatusCode());
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) TimerTask(java.util.TimerTask) Timer(java.util.Timer) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse)

Example 77 with TimerTask

use of java.util.TimerTask in project jdk8u_jdk by JetBrains.

the class Main method setupTimer.

/**
     * Set up the timer to end the test.
     *
     * @param delay the amount of delay, in seconds, before requesting
     * the process exit
     */
static void setupTimer(int delay) {
    timer = new Timer(true);
    timer.schedule(new TimerTask() {

        @Override
        public void run() {
            exitRequested = true;
        }
    }, delay * 1000);
}
Also used : Timer(java.util.Timer) TimerTask(java.util.TimerTask)

Example 78 with TimerTask

use of java.util.TimerTask in project jdk8u_jdk by JetBrains.

the class DelayOverflow method test.

void test(String[] args) throws Throwable {
    for (int how = 0; how < 4; how++) {
        final CountDownLatch done = new CountDownLatch(1);
        final AtomicInteger count = new AtomicInteger(0);
        final Timer timer = new Timer();
        final TimerTask task = new TimerTask() {

            @Override
            public void run() {
                checkScheduledExecutionTime(this);
                count.incrementAndGet();
                done.countDown();
            }
        };
        scheduleNow(timer, task, how);
        done.await();
        equal(count.get(), 1);
        checkScheduledExecutionTime(task);
        if (new java.util.Random().nextBoolean())
            sleep(10);
        check(task.cancel());
        timer.cancel();
        checkScheduledExecutionTime(task);
    }
}
Also used : Timer(java.util.Timer) TimerTask(java.util.TimerTask) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 79 with TimerTask

use of java.util.TimerTask in project jdk8u_jdk by JetBrains.

the class ZipEntryFreeTest method run.

@Override
public void run() {
    try {
        int iteration = 0;
        TimerTask tt = (new TimerTask() {

            @Override
            public void run() {
                try {
                    is.close();
                } catch (Exception ex) {
                    ex.printStackTrace(System.out);
                }
            }
        });
        timer.schedule(tt, 50);
        while (is.read() != -1 && iteration++ < 1_000) {
        }
    } catch (ZipException ze) {
        // ZipException now expected instead of ZIP_Read crash
        System.out.println(ze);
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        timer.cancel();
    }
}
Also used : TimerTask(java.util.TimerTask)

Example 80 with TimerTask

use of java.util.TimerTask in project Space-Station-Tracker by Kiarasht.

the class MapsActivity method onResume.

/**
     * Reread the refreshrate and update views if needed such as prediction line, texts and their
     * properties (Color, size, etc.).
     */
protected void onResume() {
    super.onResume();
    // Asks for a interstitial ad now so next time user starts a new activity we have it ready
    requestNewInterstitial();
    // Update decimal format
    String format = "0";
    for (int i = 0; i < mSharedPreferences.getInt("decimalType", 3); ++i) {
        if (i == 0)
            format += ".";
        format += "#";
    }
    mDecimalFormat = new DecimalFormat(format);
    // When activity was just paused
    if (mStart) {
        mRefreshrate = 1000 * (mSharedPreferences.getInt("refresh_Rate", 9) + 1);
        if (mTimer != null) {
            mTimer.cancel();
            mTimer.purge();
            mTimer = null;
        }
        // Track ISS based on refreshrate
        mTimer = new Timer();
        mTimer.scheduleAtFixedRate(new TimerTask() {

            @Override
            public void run() {
                trackISS();
            }
        }, 0, mRefreshrate);
        mMaptype();
    // When activity is killed or created for first time
    } else {
        mStart = true;
        mTimer = new Timer();
    }
    // Provide optional advertisements that is visible/hidden by a checkbox in Preferences
    if (mSharedPreferences.getBoolean("advertisement", false) && mAdView != null) {
        // User disabled ads
        mAdView.setVisibility(View.GONE);
    } else if (!mSharedPreferences.getBoolean("advertisement", false)) {
        if (mAdView == null) {
            // User wants ads but instance is null
            mAdView = (AdView) findViewById(R.id.adView);
            AdRequest adRequest = new AdRequest.Builder().addTestDevice(getString(R.string.test_device)).build();
            mAdView.loadAd(adRequest);
        } else {
            // User wants ads, instance already got one
            mAdView.setVisibility(View.VISIBLE);
        }
    }
    // Update the color and size of polylines if they are different in settings than what they are right now
    mCurrentColor = Color.YELLOW;
    mCurrentWidth = 5;
    try {
        mCurrentColor = mSharedPreferences.getInt("colorType", Color.YELLOW);
        mCurrentWidth = mSharedPreferences.getInt("sizeType", 5);
    } catch (ClassCastException e) {
        Toast.makeText(mContext, R.string.data_corrupted, Toast.LENGTH_LONG).show();
        mSharedPreferences.edit().clear().apply();
    }
    if (mPolyLine != null) {
        if (mMap != null) {
            // Color needs updating?
            if (mPolyLine.getColor() != mCurrentColor) {
                for (int i = 0; mPolyArray[i] != null && i < mPolyArray.length - 1; ++i) {
                    mPolyArray[i].setColor(mCurrentColor);
                }
            }
            // What about their size?
            if (mPolyLine.getWidth() != mCurrentWidth) {
                for (int i = 0; mPolyArray[i] != null && i < mPolyArray.length - 1; ++i) {
                    mPolyArray[i].setWidth(mCurrentWidth);
                }
            }
        } else {
            Toast.makeText(mContext, R.string.errorMap, Toast.LENGTH_SHORT).show();
        }
    }
    // If user even wants the additional info, and their properties have been changed, update them
    if (mDescription.getVisibility() == View.VISIBLE && !mSharedPreferences.getBoolean("info_ISS", true)) {
        mDescription.setVisibility(View.GONE);
    } else if (mDescription.getVisibility() == View.GONE && mSharedPreferences.getBoolean("info_ISS", true)) {
        mDescription.setVisibility(View.VISIBLE);
    }
    // Update text color if different than settings
    int textColor = mSharedPreferences.getInt("colorText", Color.YELLOW);
    if (mLatLong.getCurrentTextColor() != textColor) {
        mLatLong.setTextColor(textColor);
        mDescription.setTextColor(textColor);
    }
    // Update text outline color if different
    int textOutlineColor = mSharedPreferences.getInt("colorHighlightText", Color.BLACK);
    if (mLatLong.getShadowColor() != mSharedPreferences.getInt("colorHighlightText", Color.BLACK)) {
        mLatLong.setShadowLayer(6, 0, 0, textOutlineColor);
        mDescription.setShadowLayer(6, 0, 0, textOutlineColor);
    }
    // Update text size as well if different
    int textSize = mSharedPreferences.getInt("textSizeType", 12);
    if (mLatLong.getTextSize() != textSize) {
        mLatLong.setTextSize(textSize);
        mDescription.setTextSize(textSize);
    }
    if (mAdView != null) {
        mAdView.resume();
    }
    mSuccess = 0;
}
Also used : AdRequest(com.google.android.gms.ads.AdRequest) Timer(java.util.Timer) TimerTask(java.util.TimerTask) DecimalFormat(java.text.DecimalFormat) AdView(com.google.android.gms.ads.AdView)

Aggregations

TimerTask (java.util.TimerTask)900 Timer (java.util.Timer)653 IOException (java.io.IOException)90 Date (java.util.Date)58 Test (org.junit.Test)57 File (java.io.File)30 ArrayList (java.util.ArrayList)29 Intent (android.content.Intent)25 AtomicLong (java.util.concurrent.atomic.AtomicLong)21 Handler (android.os.Handler)20 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)20 HashMap (java.util.HashMap)17 List (java.util.List)14 TextView (android.widget.TextView)13 CountDownLatch (java.util.concurrent.CountDownLatch)13 View (android.view.View)12 Map (java.util.Map)12 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)12 InputMethodManager (android.view.inputmethod.InputMethodManager)11 Random (java.util.Random)11