java数组线程安全_Java线程组

java数组线程安全

ThreadGroup is a class which is used for creating group of threads. This group of threads are in the form of a tree structure, in which the initial thread is the parent thread. A thread can have all the information of the other threads in the groups but can have the information of the threads of the other groups. It is very useful in the case where we want to suspend and resume numbers of threads. This thread group is implemented by java.lang.ThreadGroup class.

ThreadGroup是用于创建线程组的类。 这组线程采用树结构的形式,其中初始线程是父线程。 一个线程可以具有组中其他线程的所有信息,但是可以具有其他组中线程的信息。 在我们要暂停和恢复线程数的情况下,这非常有用。 该线程组由java.lang.ThreadGroup类实现。

线程组中有两种构造函数,它们如下: (There are two types of Constructors in the Thread group they are as follow:)

1. public ThreadGroup(String name)

1. public ThreadGroup(字符串名称)

2. public ThreadGroup(ThreadGroup parent, String name)

2. public ThreadGroup(ThreadGroup的父级,字符串名称)

以下是线程组中存在的方法 (Following are the methods present in Thread group)

1. checkAccess()

1. checkAccess()

In Java, checkAccess() method is of ThreadGroup class. It is used to check whether the running thread has permission for modification or not in the ThreadGroup.

在Java中, checkAccess()方法属于ThreadGroup类。 用于检查正在运行的线程是否在ThreadGroup中具有修改权限。

Syntax

句法

public final void checkAccess()

Example:

例:

class ThreadDemo1_1 extends Thread   
{  
    ThreadDemo1_1(String a, ThreadGroup b)  
    {  
        super(b, a);  
    }  
    public void run()  
    {  
        for (int i = 0; i< 10; i++)   
        {  
            try  
            {  
                Thread.sleep(10);  
            }  
            catch (InterruptedException ex)   
            {  
                System.out.println(Thread.currentThread().getName());  
            }  
        }  
        System.out.println(Thread.currentThread().getName());  
    }  
}   
public class ThreadDemo1
{  
    public static void main(String arg[]) throws InterruptedException, SecurityException
    {  

        ThreadGroup obj1 = new ThreadGroup("Parent thread =====> ");  
        ThreadGroup obj2 = new ThreadGroup(obj1, "child thread =====> ");  
        ThreadDemo1_1 t1 = new ThreadDemo1_1("*******Thread-1*******", obj1);  
        t1.start();   
        ThreadDemo1_1 t2 = new ThreadDemo1_1("*******Thread-2*******", obj1);  
        t2.start();   
        obj1.checkAccess();  
        System.out.println(obj1.getName() + " has access");  
        obj2.checkAccess();  
        System.out.println(obj2.getName() + " has access");  
    }  
}
thread-group-example

2. activeCount()

2. activeCount()

In Java, activeCount() method is of ThreadGroup class. It is used to count the active threads in a group which are currently running.

在Java中, activeCount()方法属于ThreadGroup类。 它用于计算当前正在运行的组中的活动线程。

public static int activeCount()

Example:

例:

class Demo2 extends Thread   
{  
    Demo2 (String a, ThreadGroup b)  
    {  
        super(b, a);  
    }  
    public void run()  
    {  
        for (inti = 0; i< 10; i++)   
        {  
            try  
            {  
                Thread.sleep(10);  
            }  
            catch (InterruptedException ex)   
            {  
                Syste  m.out.println(Thread.currentThread().getName());  
            }  
        }  
            System.out.println(Thread.currentThread().getName());  
    }  
}   
public class ThreadDemo2
{  
    public static void main(String arg[])  
    {  
        ThreadGroup o1 = new ThreadGroup("parent thread group");   
        Demo2 obj1 = new Demo2 ("Thread 1 =====> ", o1);  
        Demo2 obj2 = new Demo2 ("Thread 2 =====> ", o1);  
        Demo2 obj3 = new Demo2 ("Thread 3 =====> ", o1);  
    	Demo2 obj4 = new Demo2 ("Thread 4 =====> ", o1);  
        Demo2 obj5 = new Demo2 ("Thread 5 =====> ", o1);  
        Demo2 obj6 = new Demo2 ("Thread 6 =====> ", o1); 
        obj1.start();  
        obj2.start();  
        obj3.start(); 
    	obj4.start();
    	obj5.start();
    	obj6.start();
        System.out.println("Total number of active thread =====> "+ o1.activeCount());  
    }  
}
thread-group-example-2

3. activeGroupCount()

3. activeGroupCount()

In Java, activeGroupCount() method is of ThreadGroup class. It is used to count the active groups of threads which are currently running.

在Java中, activeGroupCount()方法属于ThreadGroup类。 它用于计算当前正在运行的活动线程组。

Syntax public int activeGroupCount().

语法public int activeGroupCount()。

public int activeGroupCount().

Example:

例:

class Demo2 extends Thread   
{  
    Demo2 (String a, ThreadGroup b)  
    {  
        super(b, a);  
    }  
    public void run()  
    {  
        for (inti = 0; i< 10; i++)   
        {  
            try  
            {  
                Thread.sleep(10);  
            }  
            catch (InterruptedException ex)   
            {  
                System.out.println(Thread.currentThread().getName());  
            }  
        }  
        System.out.println(Thread.currentThread().getName()+" =====> completed executing");  
    }  
}   
public class ThreadDemo2
{  
    public static void main(String arg[])  
    {  
        ThreadGroup o1 = new ThreadGroup("parent thread group");   
        ThreadGroup o2 = new ThreadGroup(o1,"Child thread group");   
        ThreadGroup o3 = new ThreadGroup(o1,"parent thread group");   

        Demo2 obj1 = new Demo2("*****Thread 1*****",o1);
        System.out.println(obj1.getName() + " =====> starts");  

        obj1.start(); 

        System.out.println("Total number of active thread =====> "+ o1.activeGroupCount());  
    }  
}
thread-group-example

4. destroy()

4. destroy()

In Java, the destroy() method is of ThreadGroup. It used to destroy a thread group. For destroying any thread group it is important that all the threads in that group should be stopped.

在Java中, destroy()方法属于ThreadGroup。 它过去曾破坏线程组。 对于销毁任何线程组,重要的是应停止该组中的所有线程。

syntax

句法

public void destroy()

Example:

例:

class Demo2 extends Thread   
{  
    Demo2 (String a, ThreadGroup b)  
    {  
        super(b, a);  
    }  
    public void run()  
    {  
        for (inti = 0; i< 10; i++)   
        {  
            try  
            {  
                Thread.sleep(10);  
            }  
            catch (InterruptedException ex)   
            {  
                System.out.println(Thread.currentThread().getName());  
            }  
        }  
        System.out.println(Thread.currentThread().getName()+" =====> completed executing");  
    }  
}   
public class ThreadDemo2
{  
    public static void main(String arg[])  throws InterruptedException, SecurityException
    {  
        ThreadGroup o1 = new ThreadGroup("*****parent thread group*****");   
        ThreadGroup o2 = new ThreadGroup(o1,"*****Child thread group*****");   

        Demo2 obj1 = new Demo2("*****Thread 1*****",o1);  
        obj1.start(); 
        Demo2 obj2 = new Demo2("*****Thread 2*****",o1);  
        obj2.start(); 

        obj1.join();
        obj2.join();

        o2.destroy();
        System.out.println(o2.getName()+" ====> Destroyed");
        o1.destroy();
        System.out.println(o1.getName()+" ====> Destroyed");
    }  
}
thread-group-example

5. enumerate(Thread[] list)

5.枚举(Thread []列表)

In Java, enumerate() method is of ThreadGroup class. It is used for copying active threads into a specified array.

在Java中, enumerate()方法属于ThreadGroup类。 它用于将活动线程复制到指定的数组中。

public int enumerate(Thread[] array)

Example:

例:

class Demo2 extends Thread   
{  
    Demo2 (String a, ThreadGroup b)  
    {  
super(b, a);  
    }  
    public void run()  
    {  
        for (inti = 0; i< 10; i++)   
        {  
            try  
            {  
Thread.sleep(10);  
            }  
            catch (InterruptedException ex)   
            {  
System.out.println(Thread.currentThread().getName());  
            }  
        }  
System.out.println(Thread.currentThread().getName()+" =====> completed executing");  
    }  
}   
public class ThreadDemo2
{  
    public static void main(String arg[])  
    {  
ThreadGroup o1 = new ThreadGroup("*****parent thread group*****");   
ThreadGroup o2 = new ThreadGroup(o1,"*****Child thread group*****");   
	
	Demo2 obj1 = new Demo2("*****Thread 1*****",o1); 
	System.out.println("Thread 1 Starts"); 
        obj1.start(); 
	Demo2 obj2 = new Demo2("*****Thread 2*****",o1);  
	System.out.println("Thread 2 Starts"); 
        obj2.start(); 

	Thread[] tarray = new Thread[o1.activeCount()];  
int count1 = o1.enumerate(tarray);  

        for (inti = 0; i< count1; i++)   
System.out.println(tarray[i].getName() + " =====> Found");          

	}  
}
thread-group-example

6. getMaxPriority()

6. getMaxPriority()

In Java, getMaxPriority() method is of ThreadGroup class. It is used for check the maximum priority of the thread group.

在Java中,getMaxPriority()方法属于ThreadGroup类。 用于检查线程组的最大优先级。

Syntax

句法

public final int getMaxPriority()

Example:

例:

class Demo2 extends Thread   
{  
    Demo2 (String a, ThreadGroup b)  
    {  
        super(b, a);  
    }  
    public void run()  
    {  
        for (inti = 0; i< 10; i++)   
        {  
            try  
            {  
                Thread.sleep(10);  
            }  
            catch (InterruptedException ex)   
            {  
                System.out.println(Thread.currentThread().getName());  
            }  
        }  
        System.out.println(Thread.currentThread().getName()+" =====> completed executing");  
    }  
}   
public class ThreadDemo2
{  
    public static void main(String arg[])  
    {  
        ThreadGroup o1 = new ThreadGroup("*****parent thread group*****");
        System.out.println("Maximum priority of Parent Thread: " + o1.getMaxPriority());   
        ThreadGroup o2 = new ThreadGroup(o1,"*****Child thread group*****"); 
        System.out.println("Maximum priority of Child Thread: " + o2.getMaxPriority());
        Demo2 obj1 = new Demo2("*****Thread 1*****",o1); 
        System.out.println("Thread 1 Starts"); 
        obj1.start(); 
        Demo2 obj2 = new Demo2("*****Thread 2*****",o1);  
        System.out.println("Thread 2 Starts"); 
        obj2.start();           

    }  
}
thread-group-example

7. getName()

7. getName()

In Java, getName() method is of ThreadGroup class. It is used for getting the name of the current thread group.

在Java中, getName()方法属于ThreadGroup类。 它用于获取当前线程组的名称。

public final String getName()

Example:

例:

class Demo3 extends Thread   
{  
    Demo3(String a, ThreadGroup b)  
    {  
        super(b, a);  
        start();  
    }  
    public void run()  
    {  
        System.out.println(Thread.currentThread().getName());  
    }  
}   
public class ThreadDemo3
{  
    public static void main(String arg[]) throws InterruptedException,  
    SecurityException, Exception  
    {  
        ThreadGroup o1 = new ThreadGroup("*****Parent thread*****");  
        ThreadGroup o2 = new ThreadGroup(o1, "*****Child thread*****");  

        Demo3 obj1 = new Demo3("Thread-1", o1);  
        System.out.println("Name of First threadGroup : " +obj1.getThreadGroup().getName());  

        Demo3 obj2 = new Demo3("Thread-2", o2);  
        System.out.println("Name of Second threadGroup: " +obj2.getThreadGroup().getName());  
    }  
}
thread-group-example

8. getParent()

8. getParent()

In Java, getParent() method is of ThreadGroup class. It is used to get the parent thread from the thread group.

在Java中, getParent()方法属于ThreadGroup类。 它用于从线程组获取父线程。

public final ThreadGroup getParent()

Example:

例:

class Demo3 extends Thread   
{  
    Demo3(String a, ThreadGroup b)  
    {  
        super(b, a);  
        start();  
    }  
    public void run()  
    {  
        System.out.println(Thread.currentThread().getName());  
    }  
}   
public class ThreadDemo3
{  
    public static void main(String arg[]) throws InterruptedException,  SecurityException, Exception  
    {  
        ThreadGroup o1 = new ThreadGroup("*****Parent thread*****");  
        ThreadGroup o2 = new ThreadGroup(o1, "*****Child thread*****");  

        Demo3 obj1 = new Demo3("Thread-1", o1);  
        System.out.println("Thread one starting");  
        obj1.start();

        Demo3 obj2 = new Demo3("Thread-2", o2);  
        System.out.println("Thread second starting");
        obj2.start();

        System.out.println("Parent Thread Group for " + o1.getName() + " is " + o1.getParent().getName());  

        System.out.println("Parent Thread Group for " + o2.getName() + " is " + o2.getParent().getName());               
    }    
}
thread-group-example

9. interrupt()

9. interrupt()

In Java, interrupt() method is of ThreadGroup class. It is used to interrupt all the threads of the thread group.

在Java中, interrupt()方法属于ThreadGroup类。 它用于中断线程组的所有线程。

Syntax

句法

public final void interrupt()

Example:

例:

class Demo2 extends Thread   
{  
    Demo2 (String a, ThreadGroup b)  
    {  
        super(b, a);  
    }  
    public void run()  
    {  
        for (inti = 0; i< 10; i++)   
        {  
            try  
            {  
                Thread.sleep(10);  
            }  
            catch (InterruptedException ex)   
            {  
                System.out.println(Thread.currentThread().getName()+ " =====> interrupted");  
            }  
        }  
        System.out.println(Thread.currentThread().getName()+" =====> completed executing");  
    }  
}   
public class ThreadDemo2
{  
    public static void main(String arg[]) throws InterruptedException, SecurityException
    {  
        ThreadGroup o1 = new ThreadGroup("*****parent thread group*****");
        ThreadGroup o2 = new ThreadGroup(o1,"*****Child thread group*****");  

        Demo2 obj1 = new Demo2("*****Thread 1*****",o1); 
        System.out.println(obj1.getName()+"Thread 1 Starts"); 
        obj1.start();
        o1.interrupt();  

        Demo2 obj2 = new Demo2("*****Thread 2*****",o1);  
        System.out.println(obj2.getName()+"Thread 2 Starts"); 
        obj2.start();           

    }  
}
thread-group-example

10. isDaemon()

10. isDaemon()

In Java, isDaemon() method is of ThreadGroup class. It is used to check whether a thread is Daemon thread or not.

在Java中, isDaemon()方法属于ThreadGroup类。 它用于检查线程是否是守护程序线程。

Syntax

句法

public final boolean isDaemon()

Example:

例:

class Demo4 extends Thread   
{  
    Demo4(String a, ThreadGroup b)  
    {  
        super(b, a);  
    }  
    public void run()  
    {  
        for(inti = 0;i < 10;i++)   
            {  
                i++;  
            }  
        System.out.println(Thread.currentThread().getName() + " ====> Execution Finished");  
    }  
}  
public class ThreadDemo4
{  
    public static void main(String arg[]) throws InterruptedException, SecurityException, Exception  
    { 
        ThreadGroup o1 = new ThreadGroup("*****Parent thread*****");  
        Demo4 obj1 = new Demo4 ("*****Thread-1*****", o1);  
        obj1.start();   
        System.out.println("Is " + o1.getName() + " a daemon threadGroup? " + o1.isDaemon());  
    }  
}
thread-group-example

11. setDaemon(Boolean daemon)

11. setDaemon(布尔守护程序)

In Java, setDaemon() method is of ThreadGroup class. It is used to make a thread as a daemon thread.

在Java中, setDaemon()方法属于ThreadGroup类。 它用于使线程成为守护线程。

Syntax:

句法:

public final void setDaemon(boolean daemon)

Example:

例:

class Demo4 extends Thread   
{  
    Demo4(String a, ThreadGroup b)  
    {  
        super(b, a);  
    }  
    public void run()  
    {  
        for(int i = 0;i < 10;i++)   
        {  
            i++;  
        }  
        System.out.println(Thread.currentThread().getName() + " ====> Execution Finished");  
    }  
}  
public class ThreadDemo4
{  
    public static void main(String arg[]) throws InterruptedException, SecurityException, Exception  
    {  

        ThreadGroup o1 = new ThreadGroup("*****Parent thread*****");  
        o1.setDaemon(true);
        ThreadGroup o2 = new ThreadGroup("*****Child thread*****");  
        o2.setDaemon(true);

        Demo4 obj1 = new Demo4 ("*****Thread-1*****", o1);  
        obj1.start();   
        Demo4 obj2 = new Demo4 ("*****Thread-2*****", o2);  
        obj2.start();   
        System.out.println("Is " + o1.getName() + " a daemon threadGroup? " + o1.isDaemon());  
        System.out.println("Is " + o2.getName() + " a daemon threadGroup? " + o2.isDaemon());         

    }  
}
thread-group-example11.JPG

12. isDestoryed()

12. isDestoryed()

In Java, isDestroyed() method is of ThreadGroup class. It is used to check whether the thread group is destroyed or not.

在Java中, isDestroyed()方法属于ThreadGroup类。 用于检查线程组是否被销毁。

Syntax

句法

public boolean isDestroyed()

Example:

例:

class Demo4 extends Thread   
{  
    Demo4(String a, ThreadGroup b)  
    {  
        super(b, a);  
    }  
    public void run()  
    {  
        for(int i = 0;i < 10;i++)   
        {  
            i++;  
        }  
        System.out.println(Thread.currentThread().getName() + " ====> Execution Finished");  
    }  
}  
public class ThreadDemo4
{  
    public static void main(String arg[]) throws InterruptedException, SecurityException, Exception  
    {  

        ThreadGroup o1 = new ThreadGroup("*****Parent thread*****");  
        ThreadGroup o2 = new ThreadGroup("*****Child thread*****");  

        Demo4 obj1 = new Demo4 ("*****Thread-1*****", o1); 
        System.out.println("Starting Thread 1"); 
        obj1.start();   
        Demo4 obj2 = new Demo4 ("*****Thread-2*****", o2); 
        System.out.println("Starting Thread 2");  
        obj2.start();   
        if(o1.isDestroyed()==true)
            System.out.println("The Group is destroyed");
        else
            System.out.println("The Group is not destroyed");
    }  
}
thread-group-example

13. list()

13. list()

In Java, the list() method is of ThreadGroup class. It is used to getting all the information about a thread group. It is very useful at the time of debugging.

在Java中, list()方法属于ThreadGroup类。 它用于获取有关线程组的所有信息。 它在调试时非常有用。

Syntax

句法

public void list()

Example:

例:

class Demo4 extends Thread   
{  
    Demo4(String a, ThreadGroup b)  
    {  
        super(b, a);  
    }  
    public void run()  
    {  
        for(int i = 0;i < 10;i++)   
        {  
            i++;  
        }  
        System.out.println(Thread.currentThread().getName() + " ====> Execution Finished");  
    }  
}  
public class ThreadDemo4
{  
    public static void main(String arg[]) throws InterruptedException, SecurityException, Exception  
    {  

        ThreadGroup o1 = new ThreadGroup("*****Parent thread*****");  
        ThreadGroup o2 = new ThreadGroup("*****Child thread*****");  

        Demo4 obj1 = new Demo4("*****Thread-1*****", o1); 
        System.out.println(obj1.getName()+"Starting Thread 1"); 
        obj1.start();  

        Demo4 obj2 = new Demo4 ("*****Thread-2*****", o2); 
        System.out.println(obj2.getName()+"Starting Thread 2"); 
        obj2.start(); 

        System.out.println("List of parent Thread Group: " + o1.getName() + ":");  
        o1.list();
    }  
}
thread-group-example

14. ParentOf(ThreadGroup g)

14. ParentOf(ThreadGroup g)

In Java, ParentOf() method is of ThreadGroup class. It is used to check whether the current running thread is the Parent thread of which ThreadGroup.

在Java中, ParentOf()方法属于ThreadGroup类。 用于检查当前正在运行的线程是否是哪个ThreadGroup的父线程。

Syntax

句法

public final boolean parentOf(ThreadGroup g)

Example:

例:

class Demo4 extends Thread   
{  
    Demo4(String a, ThreadGroup b)  
    {  
        super(b, a);  
    }  
    public void run()  
    {  
        for(int i = 0;i < 10;i++)   
        {  
            i++;  
        }  
        System.out.println(Thread.currentThread().getName() + " ====> Execution Finished");  
    }  
}  
public class ThreadDemo4
{  
    public static void main(String arg[]) throws InterruptedException, SecurityException, Exception  
    {  

        ThreadGroup o1 = new ThreadGroup("*****Parent thread*****");  
        ThreadGroup o2 = new ThreadGroup("*****Child thread*****");  

        Demo4 obj1 = new Demo4("*****Thread-1*****", o1); 
        System.out.println(obj1.getName()+"Starting Thread 1"); 
        obj1.start();  

        Demo4 obj2 = new Demo4 ("*****Thread-2*****", o2); 
        System.out.println(obj2.getName()+"Starting Thread 2"); 
        obj2.start(); 

        boolean isParent = o2.parentOf(o1);  
        System.out.println(o2.getName() + " is the parent of " + o1.getName() +": "+ isParent);  

        isParent = o1.parentOf(o2);  
        System.out.println(o1.getName() + " is the parent of " + o2.getName() +": "+ isParent);  
    }  
}
thread-group-example

15. suspend()

15.suspend()

In Java, suspend() method is of ThreadGroup class. It is used to suspend all threads of the thread group.

在Java中, suspend()方法属于ThreadGroup类。 它用于挂起线程组的所有线程。

Syntax

句法

public final void suspend()

Example:

例:

class Demo4 extends Thread   
{  
    Demo4(String a, ThreadGroup b)  
    {  
        super(b, a);  
    }  
    public void run()  
    {  
        for(int i = 0;i < 10;i++)   
        {  
            i++;  
        }  
        System.out.println(Thread.currentThread().getName() + " ====> Execution Finished");  
    }  
}  
public class ThreadDemo4
{  
    public static void main(String arg[]) throws InterruptedException, SecurityException, Exception  
    {  

        ThreadGroup o1 = new ThreadGroup("*****Parent thread*****");  
        ThreadGroup o2 = new ThreadGroup("*****Child thread*****");  

        Demo4 obj1 = new Demo4("*****Thread-1*****", o1); 
        System.out.println(obj1.getName()+"Starting Thread 1"); 
        obj1.start();  

        Demo4 obj2 = new Demo4 ("*****Thread-2*****", o2); 
        System.out.println(obj2.getName()+"Starting Thread 2"); 
        obj2.start(); 

        o1.suspend();   
    }  
}
thread-group-example

16. resume()

16. resume()

In Java, resume() method is of ThreadGroup class.It is used to resume all threads of the thread group which were suspended.

在Java中, resume()方法属于ThreadGroup类,用于恢复线程组中所有已挂起的线程。

Syntax

句法

public final void resume()

Example:

例:

class Demo4 extends Thread   
{  
    Demo4(String a, ThreadGroup b)  
    {  
        super(b, a);  
    }  
    public void run()  
    {  
        for(int i = 0;i < 10;i++)   
        {  
            i++;  
        }  
        System.out.println(Thread.currentThread().getName() + " ====> Execution Finished");  
    }  
}  
public class ThreadDemo4
{  
    public static void main(String arg[]) throws InterruptedException, SecurityException, Exception  
    {  

        ThreadGroup o1 = new ThreadGroup("*****Parent thread*****");  
        ThreadGroup o2 = new ThreadGroup("*****Child thread*****");  

        Demo4 obj1 = new Demo4("*****Thread-1*****", o1); 
        System.out.println(obj1.getName()+"Starting Thread 1"); 
        obj1.start();
        o1.suspend();  

        Demo4 obj2 = new Demo4 ("*****Thread-2*****", o2); 
        System.out.println(obj2.getName()+"Starting Thread 2"); 
        obj2.start(); 
        o1.resume();
    }  
}
thread-group-example17.JPG

17. setMaxPriority(intpri)

17. setMaxPriority(intpri)

In Java, setMaxPriority() method is of ThreadGroup class. It is used to set the maximum priority of the thread group.

在Java中, setMaxPriority()方法属于ThreadGroup类。 它用于设置线程组的最大优先级。

Syntax

句法

public final void setMaxPriority(int pri)

Example:

例:

class Demo5 extends Thread   
{  
    Demo5(String a, ThreadGroup b)  
    {  
        super(b, a);  
    }  
    public void run()  
    {  
        for (int i = 0; i< 10; i++)   
        {  
            try  
            {  
                Thread.sleep(10);  
            }  
            catch (InterruptedException ex) {  
                System.out.println("Thread " + Thread.currentThread().getName() + " =====> Interrupted"); }  
        }  
        System.out.println(Thread.currentThread().getName() + " [Priority = " +   
                Thread.currentThread().getPriority() + "]");  
        System.out.println(Thread.currentThread().getName()+" =====> Execution finish");  
    }  
}   
public class ThreadDemo5
{  
    public static void main(String arg[]) throws InterruptedException,  
    SecurityException, Exception  
    {  
        ThreadGroup o1 = new ThreadGroup("*****Parent threadGroup*****");  
        ThreadGroup o2 = new ThreadGroup(o1, "*****Child threadGroup*****");  
        o1.setMaxPriority(Thread.MAX_PRIORITY-3);  
        o2.setMaxPriority(Thread.MIN_PRIORITY);  

        Demo5 obj1 = new Demo5("*****Thread-1*****", o1);  
        obj1.setPriority(Thread.MAX_PRIORITY);  
        System.out.println(obj1.getName() + " ====> starts");  
        obj1.start();  

        Demo5 obj2 = new Demo5("*****Thread-2*****", o2);  
        obj2.setPriority(Thread.MAX_PRIORITY);  
        System.out.println(obj2.getName() + " ====> starts");  
        obj2.start();  
    }  
}
thread-group-example

18. stop()

18. stop()

In Java, stop() method is of ThreadGroup class. It is used to stop threads of the thread group.

在Java中, stop()方法属于ThreadGroup类。 用于停止线程组的线程。

Syntax

句法

public final void stop()

Example:

例:

class Demo5 extends Thread   
{  
    Demo5(String a, ThreadGroup b)  
    {  
        super(b, a);  
    }  
    public void run()  
    {  
        for (int i = 0; i< 10; i++)   
        {  
            try  
            {  
                Thread.sleep(10);  
            }  
            catch (InterruptedException ex) {  
                System.out.println("Thread " + Thread.currentThread().getName() + " =====> Interrupted"); }  
        }  
        System.out.println(Thread.currentThread().getName() + " [Priority = " +   
                Thread.currentThread().getPriority() + "]");  
        System.out.println(Thread.currentThread().getName()+" =====> Execution finish");  
    }  
}   
public class ThreadDemo5
{  
    public static void main(String arg[]) throws InterruptedException,  
    SecurityException, Exception  
    {  
        ThreadGroup o1 = new ThreadGroup("*****Parent threadGroup*****");  
        ThreadGroup o2 = new ThreadGroup(o1, "*****Child threadGroup*****");  
        o1.setMaxPriority(Thread.MAX_PRIORITY-3);  
        o2.setMaxPriority(Thread.MIN_PRIORITY);  

        Demo5 obj1 = new Demo5("*****Thread-1*****", o1);    
        System.out.println("Thread one starting");  
        obj1.start();  

        Demo5 obj2 = new Demo5("*****Thread-2*****", o2);  
        System.out.println("Thread second starting");  
        obj2.start(); 

        o1.stop(); 
    }  
}
thread-group-example

19. toString()

19. toString()

In Java, toString() method is of ThreadGroup class. It is used to get the string representation of a thread group.

在Java中, toString()方法属于ThreadGroup类。 它用于获取线程组的字符串表示形式。

Syntax

句法

public String toString()

Example:

例:

class Demo5 extends Thread   
{  
    Demo5(String a, ThreadGroup b)  
    {  
        super(b, a);  
    }  
    public void run()  
    {  
        for (int i = 0; i< 10; i++)   
        {  
            try  
            {  
                Thread.sleep(10);  
            }  
            catch (InterruptedException ex) {  
                System.out.println("Thread " + Thread.currentThread().getName() + " =====> Interrupted"); }  
        }  
        System.out.println(Thread.currentThread().getName() + " [Priority = " +   
                Thread.currentThread().getPriority() + "]");  
        System.out.println(Thread.currentThread().getName()+" =====> Execution finish");  
    }  
}   
public class ThreadDemo5
{  
    public static void main(String arg[]) throws InterruptedException,  
    SecurityException, Exception  
    {  
        ThreadGroup o1 = new ThreadGroup("*****Parent threadGroup*****");  
        ThreadGroup o2 = new ThreadGroup(o1, "*****Child threadGroup*****");  


        Demo5 obj1 = new Demo5("*****Thread-1*****", o1);    
        System.out.println(obj1.getName() + " ====> starts");  
        obj1.start();  

        Demo5 obj2 = new Demo5("*****Thread-2*****", o2);  
        System.out.println(obj2.getName() + " ====> starts");  
        obj2.start();  
        System.out.println("String equivalent: " + o1.toString()); 

        System.out.println("String equivalent: " + o2.toString()); 
    }  
}
thread-group-example

翻译自: https://www.studytonight.com/java/java-thread-group.php

java数组线程安全

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值