Showing posts with label Thread. Show all posts
Showing posts with label Thread. Show all posts

Tuesday, October 31, 2017

Java Thread Example by extending Thread class | Java Thread Example by implementing Runnable interface

Java Thread Example by extending Thread class



package com.pkm;

import java.util.Date;

/**
 * Created by pritom on 31/10/2017.
 */
public class ThreadTest extends Thread {
    public static void main(String[] args) throws Exception {
        System.out.println("Thread called at = " + (new Date()));
        new ThreadTest().start();
    }

    public void run() {
        try {
            sleep(10000L);
        }
        catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Thread executed at = " + (new Date()));
    }
}


Java Thread Example by implementing Runnable interface


package com.pkm;

import java.util.Date;

/**
 * Created by pritom on 31/10/2017.
 */
public class RunnableTest implements Runnable {
    public static void main(String[] args) throws Exception {
        System.out.println("Thread called at = " + (new Date()));
        new Thread(new RunnableTest()).start();
    }

    @Override
    public void run() {
        try {
            Thread.sleep(10000L);
        }
        catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Thread executed at = " + (new Date()));
    }
}

So we can use "Runnable" interface, it will help us inherit another class, because if we use "Thread" inheritance then we should loose one scope to inherit another class if we need.


Thursday, June 4, 2015

Android: Update GUI field vlaue/display or show a message (toast) from Thread

Util Class


package com.pkm.android.utils.thread;

import android.app.Activity;
import android.content.Context;
import android.widget.Toast;

public class ThreadUtils {
    public static Object context = null;
    private static Activity activity;
 
    /**
     * setCurrentActivity(this) in onStart()  on each activity 
     * setCurrentActivity(this) in onResume()  on each activity
     * setCurrentActivity(null) in onPause()  on each activity 
     */
    public static void setCurrentActivity(Activity currentActivity) {
        activity = currentActivity;
    }

    public static Activity currentActivity() {
        return activity;
    }
 
    public static void message(final String message) {
        activity.runOnUiThread(new Runnable() {
            public void run() {
             Toast.makeText((Context) context, message, Toast.LENGTH_SHORT).show();
            }
        });
    }
}

Example Usage


@Override
protected void onStart() {
    super.onStart();
    ThreadUtils.setCurrentActivity(this);
    ThreadUtils.context = getBaseContext();
}

ThreadUtils.message("A message from thread, will normally fail!!!");

Wednesday, December 5, 2012

Run Multiple Java Thread

bool isFinished = false;
final ProgressDialogue dialogue = new ProgressDialogue("Loading...");
final Thread progressThread = new Thread() {
    public void run() {
        dialogue.setVisible(true);
    }
};
final Thread progressEvent = new Thread() {
    public synchronized void run() {
        /* DO SOME WORK */
        isFinished = true;
    }
};
 final Runnable doFinished = new Runnable() {
     public void run() { saveBackupFile(); }
};
final Thread progressMonitor = new Thread() {
    public void run() {
        while (true) {
            try {
                /* SLEEP SOME TIME 1000 ms */
                Thread.sleep(1000);
            } catch (Exception ex) {

            }
            /* WAIT FOR WORK FINISHED */
            if (isFinished) {
                /* FINISH ALL */
                Thread.currentThread().interrupt();
                break;
            }
        }
         /* CALL DO FINISH METHOD WHEN THIS PROCESS REACH TO END */
         SwingUtilities.invokeLater(doFinished);
    }
};
progressThread.start();
progressEvent.start();
progressMonitor.start();