Need help about Thread issue

Given from scjp
1.public static void main(String[] args) {
2.        NameRunnable nr = new NameRunnable();
3.        Thread t1 = new Thread(nr);              
4.        t1.start();
5.        t1.join();                   
}There are some questions which i made up myself to test how i understand about the thread issue and I really need some experts to confirm about it.
Question 1: At line 3, how many threads we have now ?
my answer is
there are 2 threads. the first one is main thread and the second one is t.
I am not sure about the t thread because in the book, they indicate that after instantiating the thread,
we just have a thread object but not a true thread.
Question 2: At line 4, after t1.start(), which thread will be executed first.
My answer is
we dont know because it depends on the scheduler.
Question 3: what t1.join() does ?
my answer is
blablabla.What I am thinking is t.join() joins the current thread to the end of t so that when t finishes the current thread can run again. However, which one is the current thread now. That is why I am stuck
Please help me. I appreciate
Edited by: newbie on Nov 25, 2010 4:46 PM
Edited by: newbie on Nov 25, 2010 4:47 PM
Edited by: newbie on Nov 25, 2010 4:49 PM

newbie wrote:
Given from scjp
public static void main(String[] args) {
NameRunnable nr = new NameRunnable();
Thread t1 = new Thread(nr);              
t1.start();
t1.join();                   
}There are some questions which i made up myself to test how i understand about the thread issue and I really need some experts to confirm about it.
Question 1: At line 3, how many threads we have now ?
my answer is
there are 2 threads. the first one is main thread and the second one is t.
I am not sure about the t thread because in the book, they indicate that after instantiating the thread,
we just have a thread object but not a true thread.
Don't use code blocks for prose. It won't autowrap and can lead to display problems if a line is too long.
Which is line 3?
What do you mean by "how many threads to we have?" Do you mean how many Thread objects exist, or how many threads (lower case "t") are executing.
After you have called t1.start(), there are at least 2 threads executing--the main thread, and the one you started. I say "at least" because the JVM has some administrative threads of its own.
Question 2: At line 4, after t1.start(), which thread will be execute first.
My answer is
we dont know because it depends on the scheduler.
Correct. And don't think of one executing "first." They can take turns, or they can both execute at the same time (if you have a multicore or multi-CPU machine).
Question 3: what t1.join() does ?
my answer is
blablabla.What I am thinking is t.join() joins the current thread to the end of t so that when t finishes the current thread can run again. However, which one is the current thread now. That is why I am stuck
Please help me. I appreciateDid you read [url http://download.oracle.com/javase/6/docs/api/java/lang/Thread.html#join()]the docs for join()? They tell you exactly what it does. If you don't understand after reading, post again.

Similar Messages

  • I need help about threads

    Hi!, I need to know how can i do that a father thread have acces to the son thread's methods or how a son thread can to notify at father any event?
    thanks

    look at the thread tutorial, it covers nofity, notifyAll, etc. Also check out the Thread class API...

  • Need help about below issue

    oracle version=10.2.0.4
    OS= RHEl 5.5
    Was testing for oracle locking mechanism and got something weired, need you response on the same.
    The test case was as below,
    conn scott/tiger
    SQL> create table t1 (name varchar2(10), empcode number(10), salary number(10));
    Table created.
    SQL> insert into t1 values('SARAH',100,2000);
    1 row created.
    SQL> insert into t1 values ('marry',101,2300);
    1 row created.
    SQL> commit;
    Commit complete.
    SQL> select * from t1;
    NAME   EMPCODE  SALARY
    SARAH     100       2000
    marry        101       2300
    Then using connected to database using 2 different terminals and fired the below sql cmd
    terminal 1
    update t1 set EMPCODE=200 where EMPCODE=101;
    which cause row lock on t1 and waiting for commit.
    terminal 2
    update t1 set NAME='MARRY' where salary=2300;
    which hangs, as the row was locked by the first terminal.
    Then what i did is exit the first terminal without using commit and then exit the second terminal also without commit.
    after that i connected to database using third terminal and check the value  and i found is below,
    SQL> select * from t1;
    NAME          EMPCODE     SALARY
    SARAH             100       2000
    MARRY             200       2300
    can anyone explain what happened.
    Regards,
    Saurabh

    Hi,
    "when you exit SQL*Plus, it would always commit - there was an implicit "commit work" issued for you right before it would disconnect your session"
    Ref  :The Tom Kyte Blog: Another SQL*Plus thing I learned...
    HTH

  • Need help about how to capture the name of an object before inserting.

    Hi everybody,
    how i can capture the name of an object before it inserts in a table.
    i will use a pre-insert trigger to do this, but i don't know how to specify the name of the object that's inserting.
    any help about this issue will be appreciated.

    Mentioning objects you mean: You have a procedure which inserts data into some table and this procedure needs to know the name of the caller (another procedure or ...)
    Can this be of some help http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:382218730166 ?
    Regards
    Etbin

  • Need help about Hidden Markov Model model

    I want to make classification for EEG signal using Hidden Markov Model
    algorithm based on neural network.
    plz need help about how to implement this algorithm using LABVIEW.
    if not I want another thing to make classification.
    any one know information about this topic, send me a reply
    thanks

    Have you derrived the HMM that you want to implement?
    If so, post the algorithm and we can provide comments on how to implement it using LabVIEW.
    Message Edited by Ray.R on 04-12-2010 12:54 PM

  • Need help about ref cursor using like table

    Hi Guys...
    I am devloping package function And i need help about cursor
    One of my function return sys_refcursor. And the return cursor need to be
    join another table in database . I don't have to fetch all rows in cursor
    All i need to join ref cursor and another table in sql clause
    like below
    select a.aa , b.cc form ( ref_cursor ) A, table B
    where A.dd = B.dd
    I appeciate it in advance

    My understanding is that you have a function that returns a refcursor and is called by a java app.
    Because this is a commonly used bit of code, you also want to reuse this cursor in other bits of sql and so you want to include it like table in a bit of sql and join that refcursor to other tables.
    It's not as easy as you might hope but you can probably achieve this with pipelined functions.
    Is it a direction that code should be going down? yes, eventually. I like the idea of pulling commonly used bits of code into a SQL statement especially into the WITH section, provided it could be used efficiently by the CBO.
    Is it worth the effort given what you have to do currently to implement it? possibly not.
    what else could you do? construct the sql statement independently of the thing that used it and reuse that sql statement rather than the refcursor it returns?
    Message was edited by:
    dombrooks

  • Need help about boot SUNFIRE V120

    Hi all....
    I need help about how to change boot from boot net to boot disk....
    after i installed sun solaris 10 at my sunfire v120, then reboot the machine ... this sunfire still boot from net not from disk...
    can i change it?
    please i really need help ....
    thanks

    If its booted, try "eeprom boot-device=disk"
    If your at the OBP, "setenv boot-device disk".
    If that doesnt work check the value of "diag-switch?".
    It could be booting off the the diag-device instead

  • Need Help about 3D and revolving in illustrator

    I really need help about 3D, Please tell me that How i create 3D in illustrator
    i want to revolve text or shape around any object like this, any plugin? or other software required for this?
    if somebody know this it will be really help full
       http://rcgrafix.fizwig.com/1452732-large.jpg    
       http://rcgrafix.fizwig.com/1384370-large.jpg
       http://rcgrafix.fizwig.com/1062180-large.jpg
    Thanks
    Arsi

    if links are not working try Copy and paste it in to your browser address bar
    thank you

  • Need help about the SHA Message Digest ? & what is use of Message Diagest ?

    need help about the SHA Message Digest ? & what is use of Message Diagest ?
    1>i have one program of making message digest
    2>which requires two files name
    3>one for input like txt
    4> second is out put file of message digest
    can any one tell what is the use of second generated file .

    MessageDigest md = MessageDigest.getInstance("SHA");
    FileInputStream fis = new FileInputStream(args[0]);
    byte[] b = new byte[1024];
    int readed = -1;
    while((readed = fis.read(b)) > 0)
         md.update(b, 0, readed);
    fis.close();
    FileOutputStream fos = new FileOutputStream(args[1]);
    byte[] d = md.digest();
    StringBuffer sb = new StringBuffer();
    for(int i = 0; i < d.length; i++) {
         String str = Integer.toHexString(d[i] & 0xff);
         sb.append(str.length() < 2 ? "0" + str : str);
    fos.write(sb.toString().getBytes());
    fos.close();

  • Need help about 1015

    need help about 1015 it says connect iphone to itunes on screen i did it but it doesnt work and says unexpected 1015

    Try restoring it: Make sure you are following the instructions in this procedure to the letter.  Here they are, with emphasis on some easily overlooked requirements:
    Disconnect the USB cable from the iPhone, but leave the other end of the cable connected to your computer's USB port.
    Turn off the device: Press and hold the Sleep/Wake button for a few seconds until the red slider appears, then slide the slider. Wait for the device to turn off.If you cannot turn off the device using the slider, press and hold the Sleep/Wake and Home buttons at the same time. When the device turns off, release only the Sleep/Wake button.
    Continue pressing and holding the Home button while you reconnect the USB cable to the device. The device should turn on.
    Note: If you see the "low battery" screen, let the device charge for at least ten minutes to ensure that the battery has some charge, and then start with step 2 again.
    Continue holding the Home button until you see the "Connect to iTunes" screen. When this screen appears (iTunes icon and USB connector) release the Home button.
    If necessary, open iTunes. You should see the "recovery mode" alert ("iTunes has detected a phone in recovery mode").
    Use iTunes to restore the device.
    If you do not see the "Connect to iTunes" screen, try these steps again.
    If you see the "Connect to iTunes" screen but the device does not appear in iTunes, see this article and its related links.
    If you have backed up the iPhone in the past, select the device in iTunes and choose "restore from backup".

  • Hi, Need help about ios7 upgrade, after this upgrade I cannot watch youtube or any video with my Ipad,

    Hi, Need help about ios7 upgrade, after this upgrade I cannot watch youtube or any video with Ipad, and Iphone as well
    I think my wireless rooter's setting has some problem but cannot found anything to solve,
    I can watch if there is another wireless network , I tried this option in another place who has wireless network and I can watch.
    Do you have any idea to do these setting , I dont have any problem when Ipad has 6.1.3 IOS,
    need help
    thanks

    Thanks
    I will and share the result.

  • Need help with threads?.. please check my approach!!

    Hello frnds,
    I am trying to write a program.. who monitors my external tool.. please check my way of doing it.. as whenever i write programs having thread.. i end up goosy.. :(
    first let me tell.. what I want from program.. I have to start an external tool.. on separate thread.. (as it takes some time).. then it takes some arguments(3 arguments).. from file.. so i read the file.. and have to run tool.. continously.. until there are arguments left.. in file.. or.. user has stopped it by pressing STOP button..
    I have to put a marker in file too.. so that.. if program started again.. file is read from marker postion.. !!
    Hope I make clear.. what am trying to do!!
    My approach is like..
    1. Have two buttons.. START and STOP on Frame..
    START--> pressed
    2. check marker("$" sign.. placed in beginning of file during start).. on file..
         read File from marker.. got 3 arg.. pass it to tool.. and run it.. (on separate thread).. put marker.. (for next reading)
         Step 2.. continously..
    3. STOP--> pressed
         until last thread.. stops.. keep running the tool.. and when last thread stops.. stop reading any more arguments..
    Question is:
    1. Should i read file again and again.. ?.. or read it once after "$" sign.. store data in array.. and once stopped pressed.. read file again.. and put marker ("$" sign) at last read line..
    2. how should i know when my thread has stopped.. so I start tool again??.. am totally confused.. !!
    please modify my approach.. if u find anything odd..
    Thanks a lot in advance
    gervini

    Hello,
    I have no experience with threads or with having more than run "program" in a single java file. All my java files have the same structure. This master.java looks something like this:
    ---master.java---------------------------------------------------
    import java.sql.*;
    import...
    public class Master {
    public static void main(String args []) throws SQLException, IOException {
    //create connection pool here
    while (true) { // start loop here (each loop takes about five minutes)
    // set values of variables
    // select a slave process to run (from a list of slave programs)
    execute selected slave program
    // check for loop exit value
    } // end while loop
    System.out.println("Program Complete");
    } catch (Exception e) {
    System.out.println("Error: " + e);
    } finally {
    if (rSet1 != null)
    try { rSet1.close(); } catch( SQLException ignore ) { /* ignored */ }
    connection.close();
    -------end master.java--------------------------------------------------------
    This master.java program will run continuously for days or weeks, each time through the loop starting another slave process which runs for five minutes to up to an hour, which means there may be ten to twenty of these slave processes running simultaneously.
    I believe threads is the best way to do this, but I don't know where to locate these slave programs: either inside the master.java program or separate slave.java files? I will need help with either method.
    Your help is greatly appreciated. Thank you.
    Logan

  • Need help about an unrecognized installer

    Hello,
    I need some help about installing a software. I bought a softsynth called Ultra Focus. Inside the discs, there is a folder for a Mac Installer. And the file called Ultra Focus Installer is not being recognized by Tiger. And, this file does not have an extension. Just a white file called Ultra Focus Installer. I don't know what to do to install the software. Is this a common issue?
    Thanks!

    And, I don't know what is CMD+i. Can you describe what it is?
    Sorry, CMD+i means the Apple/Command/Clover Leaf key, (depending on KB), plus the i key, it's the same as, and a Shortcut to Finder>File>Get info.
    I suggest this at the moment...
    "Try Disk Utility
    1. Insert the Mac OS X Install disc that came with your computer, then restart the computer while holding the C key.
    2. When your computer finishes starting up from the disc, choose Disk Utility from the Installer menu. (In Mac OS X 10.4 or later, you must select your language first.)
    *Important: Do not click Continue in the first screen of the Installer. If you do, you must restart from the disc again to access Disk Utility.*
    3. Click the First Aid tab.
    4. Click the disclosure triangle to the left of the hard drive icon to display the names of your hard disk volumes and partitions.
    5. Select your Mac OS X volume.
    6. Click Repair. Disk Utility checks and repairs the disk."
    http://docs.info.apple.com/article.html?artnum=106214
    Then Safe Boot from the HD, (holding Shift key down at bootup), run Disk Utility in Applications>Utilities, then highlight your drive, click on Repair Permissions, reboot.
    The usual reason why updates fail or mess things up, is if Permissions are not fixed before & after every update, with a reboot... you may get a partial update when the installer finds it doesn't have Permissions to change one obscure little part of the OS, leaving you with a mix of OS versions.
    Some people get away without Repairing Permissions for years, some for only days.
    If Permissions are wrong before applying an update, you could get mixed OS versions, if Directory is the slightest messed up, who knows!
    If many Permission are repaired, or any Directory errors are found, you may need to re-apply some the latest/biggest updates.

  • Need help for desktop issue

    Hi all, i need help urgently. my desktop has nothing on it but when i get info it shows that desktop is using over 10GB of disk space and counting. does anyone knows what is going on as my available disk space getting lesser as the day goes by.

    Hi,
    I also have issues with my desktop - the iDisk was not syncing.
    I opened the desktop folder on a window, and it showed zero items, when I have the HD and LaCie hardrives showing on the desktop. The iDisk does not show on the desktop as it should. However, the desktop folder showed other items such as files saved on the desktop.
    Here's what happened last week: I received an e-mail from an airline saying that my ticket was approved - it was from an actual airline so i made the BIG mistake of opening the link only to find a bunch of scrambled characters - i immediately figured out that it was a bomb/virus/scam to destroy the computer, and after that i had problems with Entourage (had to call support to solve the issue) and now with syncing iDisk the 0-items desktop folder. Are they related or just coincidence? Who knows....
    And here's the sad part: I tried your suggestion and it didn't work. I opened the desktop folder and still shows 0 items; the iDisk still not appearing on the desktop automatically (as it should) and still "failed to sync"; and I am still frazzled/puzzled.
    Here's the messages in the terminal:
    Last login: Sun Sep 23 08:04:14 on console
    Welcome to Darwin!
    [Tocantinss-Computer:~] tc%
    [Tocantinss-Computer:~] tc% ls -alh desktop
    total 1088
    drwx------ 6 tc staff 204B Sep 22 14:21 .
    drwxr-xr-x 25 tc staff 850B Sep 23 08:04 ..
    -rw-rw-rw- 1 tc staff 42K Sep 22 14:21 .DS_Store
    -rw-rw-rw- 1 tc staff 496K Sep 20 2003 .FBCIndex
    drwxrwxrwx 3 tc staff 102B Sep 20 2003 .FBCLockFolder
    -rw-r--r-- 1 tc staff 20B May 2 2002 Icon
    [Tocantinss-Computer:~] tc%
    [Tocantinss-Computer:~] tc% defaults write com.apple.finder AppleShowAllFiles -boolean true; killall Finder
    [Tocantinss-Computer:~] tc% defaults write com.apple.finder AppleShowAllFiles -boolean true;killall Finder
    [Tocantinss-Computer:~] tc% defaults write com.apple.finder AppleShowAllFiles -boolean false;killall Finder
    [Tocantinss-Computer:~] tc%
    Do you have any other suggestion or solution?
    Thank you so much.

  • Need Help about my email folder on my curve 8520

    Need help
    How to use the same folder email on my blackberry?
    When I change my mobile network and register again my bb and setup email again with my old email, my bb will make a new folder to store my email.
    How can I change my bb setting still using my old email folder?
    Thank you
    Solved!
    Go to Solution.

    Yep, different moble providers, different BIS BlackBerry Internet Service accounts.
    I dont understand the problem. Look at your main Messages folder. Press the Menu key > Inbox Settings, can you select there which email accounts go to the main Messages folder?
    1. If any post helps you please click the below the post(s) that helped you.
    2. Please resolve your thread by marking the post "Solution?" which solved it for you!
    3. Install free BlackBerry Protect today for backups of contacts and data.
    4. Guide to Unlocking your BlackBerry & Unlock Codes
    Join our BBM Channels (Beta)
    BlackBerry Support Forums Channel
    PIN: C0001B7B4   Display/Scan Bar Code
    Knowledge Base Updates
    PIN: C0005A9AA   Display/Scan Bar Code

Maybe you are looking for