简体   繁体   中英

thread.run() works and thread.start() not works

Following is my main class.

public class ShareData {

    /**
     * @param args
     */
    public static void main(String[] args) {
        ShareReader aShareReader = new ShareReader("http://test.com:9000", "dilip.id@gmail.com", "password");
        Thread fileThread = new Thread(aShareReader);
        fileThread.run(); // fileThread.start() not calling the run() method
    }
}

If I type fileThread.run() run method is called. If I call fileThread.start() the run metod is not called. Following is my thread class. I dont know what I am doing wrong.

public class ShareReader implements Runnable {

    private String itsShareURL =    null;
    private String itsUserId        =   null;
    private String itsPassword      =   null;
    private String itsAuthToken     =   null;
    private String itsLoginURL      =   null;
    private String itsChannelUpateURL   =   null;


    /**
     * 
     */
    public ShareReader(String theShareURL, String theUserId, String thePassword) {
        this.itsShareURL    =   theShareURL;
        this.itsUserId          =   theUserId;
        this.itsPassword        =   thePassword;
        this.itsLoginURL        =   itsShareURL + "/v1.0-SNAPSHOT/login";
        this.itsChannelUpateURL =   itsShareURL + "/v1.0-SNAPSHOT/updateChannelSubscription/";
    }

    public void run() {
        JSONObject json;
        JSONArray jsonArray;
itsAuthToken = getToken(itsUserId, itsPassword);
    updateChannelList(itsAuthToken);

    String aURL = "http://test.com:9000/v1.0-SNAPSHOT/userTimeline/"+itsAuthToken+"/";
    try {
        String lat = null;
        String lon = null;
        String udid = null;
        String dateTime =  null;
        String eventID = null;
        aEventBean = new EventBean();
        jsonArray = readJsonArrayFromUrl(aURL);
        for (int i = 0; i < jsonArray.length(); i++) {
            json = jsonArray.getJSONObject(i);
            lat = json.getString("lat");
            lon = json.getString("lon");
            udid = json.getString("udid");
            eventID = json.getString("eventId");
            dateTime = json.getString("dateTime");
            aEventBean.setItsLatitude(lat);
            aEventBean.setItsLongitude(lon);
            aEventBean.setItsUDID(udid);
            aEventBean.setItsEventIdentifier(eventID);
            aEventBean.setItsDateTime(dateTime);
            System.out.println(udid + " ---> " +lat + " ==== " + lon);
            sendData(aEventBean);
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block

        e.printStackTrace();
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 
}
}

Sorry If I ask so basic question.. Ideally I need to do fileThread.start() to start a thread.. Thanks in advance...

run() is definitely called if you call start() on fileThread . Check your implementation of run() - its very likely that this method completes or terminates before your check for the print statements. Just an fyi, fileThread.run() is a sequential call while fileThread.start() is a parallel call.

Another vague possibility is that you're not implementing Java's runnable; instead, that may be some custom Runnable class in your project.

EDIT:

So apparently calling fileThread.join() helped you fix your problem, but why does this work? If you call fileThread.join() , the main thread waits until the target (in this case, your fileThread object) terminates.

fileThread.run() never starts a new thread. To start a new thread you have to call fileThread.start().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM