Find out how many threads in a Java program

Why do I get ID 20, ID 21, ID 22 by runing the following code? I would like to know if i acturely create a thread then stop it probably. somehow i need to call it two more times in my program.
start() and interrrupt() are default thread methods. when I call start() it goes to my run() method. I am worry about if the thread 20 and 21 are still doing something after creating thread 22
main = new Thread(this);
        System.out.println("ID " + main.getId());
        main.start();
        main.interrupt();
        main = null;
        main = new Thread(this);
        System.out.println("ID " + main.getId());
        main.start();
        main.interrupt();
        main = null;
        main = new Thread(this);
        System.out.println("ID " + main.getId());
        main.start();
        main.interrupt();
        main = null;

Here is my code, I would like to know if I implement my threads nicely here, I am worrying those threads are still alive, they should be interrupted. but why I got different thread ID, when i resume it?
// Two player board game play in turn. Human plays first as red, the computer plays
// second as blue. I used thread here for repainting the applet, because the computer
// evaulation function is quite slow, the gui won't draw human player's move until it
// finishs calculating computer's move. the solution i use is interrupting the current
// thread, let the gui repaint before calculating the computer move.
// Board.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Board extends Canvas implements Runnable, MouseListener {
    private Thread main;
    private int state = 0;
    private double aMove = 0;
    public void init() {
        addMouseListener(this);
    public void run() {
        removeMouseListener(this);
        main.interrupt();
        main = null;
        repaint();
        // calculate computer's move, then making move
        // this is a slow evalution function goes here
        for (int i = 0; i <10000000; i++) {
            aMove = Math.pow(0.1,1000000000);
        state = 1;
        repaint();
        System.out.println("Blue makes a move on postion " + aMove);
        state = -1;
        addMouseListener(this);
    public void paint(Graphics g) {
        System.out.println("paint");
        if (state == 1) {
            g.setColor(Color.blue);
            g.fillOval(0, 0, 20, 20);
        if (state == -1) {
            g.setColor(Color.red);
            g.fillOval(0, 0, 20, 20);
    public void mouseReleased(MouseEvent e) {
        // human player makes a move on postion 0;
        aMove = 0;
        System.out.println("Red makes a move on postion " + aMove);
        state = -1;
        repaint();
        // Why the following gives me different thread here every time?
        main = new Thread(this);
        System.out.println("Thread ID: "+ main.getId());
        main.start();
    public void mousePressed(MouseEvent e) {
    public void mouseEntered(MouseEvent e) {
    public void mouseExited(MouseEvent e) {
    public void mouseClicked(MouseEvent e) {
// appletdemo.java
import java.applet.*;
import java.awt.*;
public class appletdemo extends Applet{
    Board b = new Board();
    public void init() {
        System.out.println("start ");
        setLayout(new BorderLayout());
        this.add(b);
        b.init();
        setVisible(true);
}

Similar Messages

  • How to find out how many memory is used for NIO

    is there any way to find out how many memory is used for nio in a virtual machine. ?
    I know that is possible to find out how many total, free and used memory the heap has by using java.lang.Runtime#totalMemory...
    We sometimes expect errors that it is not possible to create any more connections because of the NIO memory size but the heap is still ok. Today we had an error where 170mbytes of heap where left but it was not possible to create new tcp connections because of an exception:
    javax.naming.NamingException: java.net.SocketException: No buffer space available (maximum connections reached?): connect
    The number of TCP-Connections was ok (<500).
    It would be very nice if someone knows how to find out how the amount of memory that NIO buffers use can be determined within Java 1.4.
    best regards
    benjamin

    We have to use 1.4.2_03. This is because of the special environment we are running on.
    We did also figured out that there are some NIO Memory Bugs but we since we are not allowed to upgrade the used Virtual Machine we have to live with these errors.
    The problem that we currently have is within a Server and because of this circumstance we are trying to find a workaround or way to find out how many memory is allocated. If we would know how to get this value we could restart the servers as a workaround after the value reaches a threshold.
    We are also very sad that we can`t easily upgrade/update to the latest 1.4.2 Version or the newest 1.5 to get rid of these NIO Bugs!

  • A way to find out how many screens/windows are open

    hi everyone,
    is there a way to find out how many sap screens are currently open? i need to know this because my program should react differently if the user has all 6 screens opened or if he has only one screen open.
    <each thread here has the same priority!>
    thanks in advance!
    Edited by: Thomas Zloch on Apr 12, 2010 3:59 PM

    Try calling function module TH_USER_INFO.
    Thomas

  • Using powershell to find out how many people are using the on-prem SkyDrive

    Hi
    Is there a way of using powershell to find out how many people are using the on-prem SkyDrive?
    Thank you.

    Hi,
    According to your post, my understanding is that you wanted to use PowerShell to find out the users who use the on premise SkyDrive.
    As this is the forum for SharePoint Server, I recommend you post your question to the forum for PowerShell or SkyDrive.
    Windows PowerShell forum:
    http://social.technet.microsoft.com/Forums/windowsserver/en-US/home?forum=winserverpowershell
    SkyDrive forum:
    http://answers.microsoft.com/en-us/onedrive/forum/sdsignin?tab=Threads
    Thanks,
    Jason
    Forum Support
    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact
    [email protected]
    Jason Guo
    TechNet Community Support

  • How do I find out how many files I have, as an earlier version of iTunes told you at the bottom of the screen. Can you please help me.

    How do I find out how many files I have, as an earlier version of iTunes told you at the bottom of the screen.
    Can anyone please help me with this so I know how many music files or video files I have in my collection.

    Have a look here...
    http://osxdaily.com/2012/11/29/5-tips-make-itunes-look-normal/

  • Is there a way of finding out how many active hours you've worked on a Premiere project?

    If not, it'd be a very cool addition for Adobe to implement... so you know exactly how many hours a project is taking (and how much to charge clients etc.) Obviously we're all prone to leaving Premiere on and wandering away, so I guess it would need to be able to differentiate between active and non-active time...

    Is there a way of finding out how many active hours you've worked on a Premiere project?
    There are two things a client get's billed for, my time, and my computer's time.  If either one of us are working on a project, the client get's billed.  Even if Adobe spent (wasted?) time on adding such a feature, they'd never be able to include any time I've spent working on the project with PP closed, so this feature would have very, very, VERY limited usefulness.
    The method suggested by the images is still the best way.

  • Am I able to find out how many times my ICloud account has been accessed?

    Am I able to find out how many times my ICloud account has been accessed?

    I'm afraid iCloud doesn't provide access logs.

  • Need to find out how many users are using Siebel Application

    Hello all,
    I need to find out how many users are using Siebel Application?
    Is there a way I can find out?
    Thanks.
    yasun

    Hi,
    Read the following:
    http://docs.oracle.com/cd/B40099_02/books/SysDiag/SysDiagSysMonitor30.html
    http://docs.oracle.com/cd/B40099_02/books/SysDiag/SysDiagSysMonitor17.html
    For full information about server status and statistics red:
    http://docs.oracle.com/cd/B40099_02/books/SysDiag/SysDiagSysMonitor.html
    T.Abed

  • HT3939 how do i find out how many gb my iphone 4 has without a sim?

    how do i find out how many gb my iphone 4 has without a sim?

    Check it through itunes by connecting your device to the computer. Go my device's in the library panel and double click device scroll to bottom of screen and it will show you.

  • How can I find out how many podcasts have been downloaded?

    My friend and I have been making our podcasts available on Itunes. Is there any way to find out how many people have downloaded?

    Log into your iTunes account on a Mac or PC and check the Purchase History in your account profile information.

  • How do i find out how many computers that I have authorized on my Itunes id

    How do i find out how many computers is authorized on my Itunes account.  I know i can have 5 computers, but i need to know where i can go to see how many are authorized.

    it was a pain but i finally got my answer from a 2006 post by WilliamGrey1.  Thanks

  • Find out how many times the application was touched by a particular user

    Hello,
    can anyone please help me out with this issue.
    I want to find out how many times the application was accessed by a particular user,their session IDs for each of the applications in a particular workspace. I am using a role based authorization scheme for my application. I am using Oracle APEX 3.2 version. Are their any underlying APEX tables/Views to find out the above details.
    thanks,
    Orton

    Hi,
    Have you check views APEX_WORKSPACE_ACCESS_LOG and APEX_WORKSPACE_ACTIVITY_LOG
    Regards,
    Jari
    Edited by: jarola on Oct 20, 2010 7:37 AM
    This might help also
    http://download.oracle.com/docs/cd/E14373_01/appdev.32/e11838/advnc.htm#CHDDHGJI

  • Can I find out how many clients are connected to a server process?

    Hi, I am hoping I can find out how many clients are connected to a server process. This could be either a programmatic lookout number, a number in a log file etc.
    By the way, does lookout have any application (not data) logs? I have noticed that occasionally lookout will crash and I cannot find any logs to determine why, what processes were running, who last accessed it etc.
    Thanks

    Hello,
    There's currently no way to acquire this data. I think that'd be a great suggestion for the product. Please submit your feedback to our developers at the following link: http://digital.ni.com/applications/psc.nsf/default?OpenForm&temp1=&node=
    Best regards,
    Yusuf C
    Application Engineer
    National Instruments

  • How can we find out how many programs are stored in a percticular package

    Hi,
    I need to find out how many programs are stored in aperticular package.
    For example we are having one package in our project. I need to give all saved programs details into this project. How can we find this.
    Thanks in advance.

    Hi,
    From TADIR table we can find out.
    Regards,
    Jyothi CH.

  • Find out how many records are updated.

    Hello,
    I am executing a Update Statement in a loop from a JSP page this is working good.
    Suppose if there are 10 records and I update 4 records, it gets updated fine.
    What I now need is to find out how many records have been updated. How can I know this?
    Thanks

    statement.executeUpdate() returns the number of records updated.
    Sai Pullabhotla

Maybe you are looking for