Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
线程行为:Java初学者_Java_Multithreading - Fatal编程技术网

线程行为:Java初学者

线程行为:Java初学者,java,multithreading,Java,Multithreading,我试图编写一个使用线程的程序,但无法理解o/p。我有两个线程:s和t。但是我看不到线程s正在工作 有人能给我解释一下这种行为吗?谢谢 我的代码: public class Bground implements Runnable { Thread t,s; Bground() { t = new Thread(this,"first thread"); s = new Thread(this,"second thread");

我试图编写一个使用线程的程序,但无法理解o/p。我有两个线程:
s
t
。但是我看不到线程
s
正在工作

有人能给我解释一下这种行为吗?谢谢

我的代码:

public class Bground implements Runnable
{
    Thread t,s;

    Bground()
    {
        t = new Thread(this,"first thread");
        s = new Thread(this,"second thread");

        s.start();
        t.start();
    }

    public void run()
    {
        System.out.println("inside run" + t.getName());
        try
        {
            for (int i = 0; i < 5; i++)
            {
                System.out.println("In child thread" + i);
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    public static void main(String argv[])
    {
        Bground b = new Bground();
        try
        {                
            for (int i = 0; i < 5; i++)
            {
                System.out.println("In main thread" + i);
                Thread.sleep(1);
            }
        }
        catch(InterruptedException e)
        {
            e.printStackTrace();
        }
    }
}
public类Bground实现可运行
{
螺纹t,s;
Bground()
{
t=新螺纹(该“第一螺纹”);
s=新螺纹(该“第二螺纹”);
s、 start();
t、 start();
}
公开募捐
{
System.out.println(“内部运行”+t.getName());
尝试
{
对于(int i=0;i<5;i++)
{
System.out.println(“子线程内”+i);
}
}
捕获(例外e)
{
e、 printStackTrace();
}
}
公共静态void main(字符串argv[])
{
Bground b=新Bground();
尝试
{                
对于(int i=0;i<5;i++)
{
System.out.println(“主线程内”+i);
睡眠(1);
}
}
捕捉(中断异常e)
{
e、 printStackTrace();
}
}
}
O/p:

c:\ProgramFiles\Java\jdk1.6.0\u 23\bin>JavaBGround

在主线程中0
内部运行第一个线程
内部运行第一个线程
在子线程0中
在子线程1中
在子线程2中
在主线程1中
在子线程3中
在子线程0中
在子线程1中
在子线程2中
在子线程3中
在子线程4中
主螺纹中2
在子线程4中
在主螺纹3中
在主螺纹4中


您正在打印两次线程“t”的名称。这就是问题所在。另一方面,这些线对我来说似乎很管用。 您可能希望改用以下代码:

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


public class Bground implements Runnable {
int name;

Bground(int name) {
    this.name=name;
}

public void run() {
    System.out.println("inside run" + name);
    try {
        for (int i = 0; i < 5; i++) {
            System.out.println("In child thread" + i);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static void main(String argv[]) {
    Bground b = new Bground(1);
    Bground b2 = new Bground(2);

    ExecutorService es = Executors.newFixedThreadPool(2);
    es.submit(b);
    es.submit(b2);
    synchronized (es) {
        es.notifyAll();
    }

    try {
        for (int i = 0; i < 5; i++) {
            System.out.println("In main thread" + i);
            Thread.sleep(1);
        }
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

}
导入java.util.concurrent.ExecutorService;
导入java.util.concurrent.Executors;
公共类Bground实现了Runnable{
int名称;
Bground(整数名){
this.name=name;
}
公开募捐{
System.out.println(“内部运行”+名称);
试一试{
对于(int i=0;i<5;i++){
System.out.println(“子线程内”+i);
}
}捕获(例外e){
e、 printStackTrace();
}
}
公共静态void main(字符串argv[]){
Bground b=新的Bground(1);
Bground b2=新的Bground(2);
Executors服务es=Executors.newFixedThreadPool(2);
提交(b);
提交(b2);
已同步(es){
es.notifyAll();
}
试一试{
对于(int i=0;i<5;i++){
System.out.println(“主线程内”+i);
睡眠(1);
}
}捕捉(中断异常e){
e、 printStackTrace();
}
}
}

正在打印两次线程“t”的名称。这就是问题所在。另一方面,这些线对我来说似乎很管用。 您可能希望改用以下代码:

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


public class Bground implements Runnable {
int name;

Bground(int name) {
    this.name=name;
}

public void run() {
    System.out.println("inside run" + name);
    try {
        for (int i = 0; i < 5; i++) {
            System.out.println("In child thread" + i);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static void main(String argv[]) {
    Bground b = new Bground(1);
    Bground b2 = new Bground(2);

    ExecutorService es = Executors.newFixedThreadPool(2);
    es.submit(b);
    es.submit(b2);
    synchronized (es) {
        es.notifyAll();
    }

    try {
        for (int i = 0; i < 5; i++) {
            System.out.println("In main thread" + i);
            Thread.sleep(1);
        }
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

}
导入java.util.concurrent.ExecutorService;
导入java.util.concurrent.Executors;
公共类Bground实现了Runnable{
int名称;
Bground(整数名){
this.name=name;
}
公开募捐{
System.out.println(“内部运行”+名称);
试一试{
对于(int i=0;i<5;i++){
System.out.println(“子线程内”+i);
}
}捕获(例外e){
e、 printStackTrace();
}
}
公共静态void main(字符串argv[]){
Bground b=新的Bground(1);
Bground b2=新的Bground(2);
Executors服务es=Executors.newFixedThreadPool(2);
提交(b);
提交(b2);
已同步(es){
es.notifyAll();
}
试一试{
对于(int i=0;i<5;i++){
System.out.println(“主线程内”+i);
睡眠(1);
}
}捕捉(中断异常e){
e、 printStackTrace();
}
}
}

您希望打印出什么内容:

System.out.println("inside run " + t.getName());

这里没有得到当前线程的名称,而是始终得到t线程的名称。要修复--获取当前线程并对其调用
getName()

您希望打印出什么内容:

System.out.println("inside run " + t.getName());

这里没有得到当前线程的名称,而是始终得到t线程的名称。修复-获取当前线程并调用<代码> GETNAMEL()/代码>。

只是让你知道:每当你对代码进行格式化时,上帝杀死一只小猫。考虑让每个线程都有一个<代码>线程。睡眠(1000)< /代码>。参数是毫秒。只要让你知道:每当你这样编写代码时,上帝杀死一只小猫。考虑让每个线程都有一个<代码>线程。睡眠(1000)< /代码>。争论是以毫秒为单位的。是的,很抱歉这太愚蠢了。我证明了我是个初学者,力I??)是的,很抱歉这太傻了……我证明了我是个初学者,力我??)