Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / February 2008

Tip: Looking for answers? Try searching our database.

Using Runnable Threads

Thread view: 
christopher_board@yahoo.co.uk - 18 Feb 2008 16:36 GMT
Hi all,

I am fairly new to java but am developing a java application that will
enable the user to shutdown a room of computers down remotely. It is
shutting down computers by using the feature within Windows via the
command prompt. However when doing it through my program what happens
is if a computer has been selected to shutdown but that computer has
no network connection or is turned off the program waits for 40
seconds before sending the shutdown request onto the next computer.
What i want the program to do is for each computer it has to go it has
to be threaded so the program can continue to send the shutdown
requests onto all the computer while at the same time waiting for
other computer to try and get a connection. This is the code that I
have used so far in order to create the Thread.

package remoteshutdown;

import java.io.BufferedInputStream; import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream; import java.io.InputStreamReader; import
java.io.RandomAccessFile; import java.text.DateFormatSymbols; import
java.util.Calendar;

public class ShutdownTest {

    public void shutdown() { System.out.println("gone into public void
        shutdown();"); if
        (remoteshutdown.mainScreen.chkShutdownMsg.isSelected()) { System.out
        .println("Inside if statement, about to start new thread"); Runnable
        runnable = new normalShutdown(); Thread thread = new
Thread(runnable); }
        }

    class normalShutdown implements Runnable {

        public void run() { System.out.println("performing shutdown");
String
            choice = (String) mainScreen.lstComputerNames
            .getSelectedValue(); System.out.println(choice);

            // will be -1 if there aare none or muliple selections. int
            which = mainScreen.lstComputerNames.getSelectedIndex();
            System.out.println(which);

            // detecting multiple selections System.out.println("--
            multiples--");

            Object[] choices =
            mainScreen.lstComputerNames.getSelectedValues(); for (Object
            aChoice : choices) { String cmd; cmd = "shutdown -m \\\\" +
            aChoice + " -s -f"; try { Runtime r = Runtime.getRuntime();

                    Process p = r.exec(cmd); InputStream i_stream =
                    p.getInputStream(); InputStreamReader reader =
                    new InputStreamReader(i_stream);

                    InputStream out = new BufferedInputStream(p
                            .getInputStream());

                    String s = null; String dayNames[] = new
                    DateFormatSymbols().getWeekdays(); Calendar
                    date2 = Calendar.getInstance();
                    System.out.println("Today is a " +
                    dayNames[date2.get(Calendar.DAY_OF_WEEK)]); File
                    f = new File( "C:\\Documents and Settings\\All
                    Users\\Application Data\\Remote
                    Shutdown\\ShutdownLog(" +
                    dayNames[date2.get(Calendar.DAY_OF_WEEK)] +
                    ").html"); RandomAccessFile raf = new
                    RandomAccessFile(f, "rw"); raf.seek(f.length());
                    BufferedReader bf_reader = new
                    BufferedReader(reader); while ((s =
                    bf_reader.readLine()) != null) {
                    raf.writeChars("<B><FONT COLOR=RED>" + aChoice +
                    " failed to shutdown: " + s + "</FONT></B>
                    <BR>"); raf.close(); byte[] b = new byte[1024];
                    System.out.print(s); } p.waitFor(); p.destroy();
                    while ((s = bf_reader.readLine()) == null) {
                    raf.writeChars("<B>" + aChoice + " Shutdown
                    successfully</B><BR>"); raf.close(); }
                    p.waitFor(); p.destroy(); } catch (Exception e)
                    { System.out .println("execution error" + cmd +
                    e.getMessage()); } }

        }

    } }

Appologise if it is not very clear. This is what the code is doing. It
is going into the public void section fine and is going down to the
bottom of that function in order to create the new thread. However the
program is not creating the Thread and therefore the Runnable section
of the program is not being executed. Thanks for any one who can off
me any help in this matter.

Any help in this matter would be highly appreciated.

Thank you
Knute Johnson - 18 Feb 2008 18:03 GMT
> Hi all,
>
[quoted text clipped - 94 lines]
>
> Thank you

I don't see where you start the thread.

Signature

Knute Johnson
email s/nospam/knute/

rossum - 18 Feb 2008 18:04 GMT
>Hi all,
>
[quoted text clipped - 27 lines]
>        Runnable runnable = new normalShutdown();
>        Thread thread = new Thread(runnable);
        //At this point you need to start your thread:
       thread.start();

>      }
>    }
[quoted text clipped - 12 lines]
>program is not creating the Thread and therefore the Runnable section
>of the program is not being executed.
I suspect that the thread is being created, however it is not being
started so it won't actually do anything.

The Sun tutorials can be helpful:
http://java.sun.com/docs/books/tutorial/essential/concurrency/runthread.html

rossum
Lew - 19 Feb 2008 00:41 GMT
> public class ShutdownTest {
>
[quoted text clipped - 5 lines]
> Thread(runnable); }
>         }

Others have pointed out that you never start()ed the Thread.

>             Object[] choices =
>             mainScreen.lstComputerNames.getSelectedValues(); for (Object
[quoted text clipped - 4 lines]
>                     p.getInputStream(); InputStreamReader reader =
>                     new InputStreamReader(i_stream);

Dude!

Please stop using TAB characters to indent Usenet posts, and do start
indenting your listings properly.

Please, again.  The listing you posted is all but unreadable.

Indentation for Usenet source listings should be no more than four spaces, and
use spaces.  This is not new news for you.  If you want people to help you, it
is only courteous to help them actually be able to read your listings.  Don't
you agree?

Signature

Lew

Nigel Wade - 19 Feb 2008 09:39 GMT
[some totally unreadable code]

Besides the fact that, as others have pointed out, you don't actually start the
one thread you do create, that one thread won't solve your problem. You need
one thread per client to make any real difference.

Signature

Nigel Wade, System Administrator, Space Plasma Physics Group,
           University of Leicester, Leicester, LE1 7RH, UK
E-mail :    nmw@ion.le.ac.uk
Phone :     +44 (0)116 2523548, Fax : +44 (0)116 2523555



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.