线程相关,别笑

caibin_caibin 2013-07-26 02:56:17
我想程序在执行的过程中去调用别人的方法,并且要传参数过去,但是我不想程序在这里等结果,而是继续往下执行,我知道用线程,但是我不会啊,我现在正在看,希望您能说的明白点,新手,别笑
...全文
196 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
caibin_caibin 2013-07-26
  • 打赏
  • 举报
回复
引用 14 楼 shnulaa 的回复:
//现在有种情况,这里调用的test()存在调用超时的情况,意思就是对方服务器没有响应 结果 //那么我现在就要做一个调用超时的处理,请问我这里怎么写 1 设置为线程为守护线程

t.setDaemon(true);
2 join的时候加上timeout时间

t.join(5000);
真心学到了,看了文档,全是一些理论的东西,一时半会还没明白,现在豁然开朗,谢谢你,继续学习
晓风吹雾 2013-07-26
  • 打赏
  • 举报
回复
//现在有种情况,这里调用的test()存在调用超时的情况,意思就是对方服务器没有响应 结果 //那么我现在就要做一个调用超时的处理,请问我这里怎么写 1 设置为线程为守护线程

t.setDaemon(true);
2 join的时候加上timeout时间

t.join(5000);
//还有上面的代码有什么问题吗? ->没有什么问题 以下是全部代码,doSomething sleep 10s, 但是join的时间是5s,那么在5s之后主线程和子线程都会结束。 5s就是timeout超时时间。5s后子线程还没有醒过来。

import java.lang.reflect.Method;

public class AsyncCall {

    /**
     * @param args
     * @throws InterruptedException
     */
    public static void main(String[] args) throws InterruptedException {
        // create the thread
        final Thread t = new Thread(new Worker(new Object[] { "string1",
                "string2" }, new Class[] { String.class, String.class },
                OtherClass.class, "doSomething"));
        t.setName("async_call_method_thread");
        t.setDaemon(true);

        System.out.println("ready to call method asynchronous ");
        // call the method asynchronize
        t.start();
        System.out.println("end to call method ");

        System.out.println("wait for the method finish");
        t.join(5000);
        System.out.println("the method has finished");
    }

    static class Worker implements Runnable {
        private final Object[] methodArguments;
        private final Class<?>[] methodClazz;
        private final Class<?> clazz;
        private final String methodName;

        private Worker(Object[] arguments, final Class<?>[] methodClazz,
                final Class<?> clazz, final String methodName) {
            this.methodArguments = arguments;
            this.methodClazz = methodClazz;
            this.clazz = clazz;
            this.methodName = methodName;
        }

        @Override
        public void run() {
            try {
                final Object o = clazz.newInstance();
                final Method m = clazz.getMethod(methodName, methodClazz);
                m.setAccessible(true);
                m.invoke(o, methodArguments);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

}

class OtherClass {
    /**
     * doSomething
     * 
     * @throws InterruptedException
     */
    public void doSomething(final String str1, final String str2)
            throws InterruptedException {
        System.out.println("i am do something, parameter1:" + str1
                + ", parameter2" + str2);

        System.out.println("i am ready to sleep 2 sec..");
        Thread.sleep(10000);
        System.out.println("i has been awaken..");
    }
}
caibin_caibin 2013-07-26
  • 打赏
  • 举报
回复
引用 12 楼 shnulaa 的回复:
[quote=引用 9 楼 caibin_caibin 的回复:] [quote=引用 7 楼 caibin_caibin 的回复:] [quote=引用 6 楼 shnulaa 的回复:] 写的一个简单的例子 非同期调用方法

import java.lang.reflect.Method;

public class AsyncCall {

    /**
     * @param args
     * @throws InterruptedException
     */
    public static void main(String[] args) throws InterruptedException {
        // create the thread
        final Thread t = new Thread(new Worker(new Object[] { "string1",
                "string2" }, new Class[] { String.class, String.class },
                OtherClass.class, "doSomething"));
        t.setName("async_call_method_thread");

        System.out.println("ready to call method asynchronous ");
        // call the method asynchronize
        t.start();
        System.out.println("end to call method ");

        System.out.println("wait for the method finish");
        t.join();
        System.out.println("the method has finished");
    }

    static class Worker implements Runnable {
        private final Object[] methodArguments;
        private final Class<?>[] methodClazz;
        private final Class<?> clazz;
        private final String methodName;

        private Worker(Object[] arguments, final Class<?>[] methodClazz,
                final Class<?> clazz, final String methodName) {
            this.methodArguments = arguments;
            this.methodClazz = methodClazz;
            this.clazz = clazz;
            this.methodName = methodName;
        }

        @Override
        public void run() {
            try {
                final Object o = clazz.newInstance();
                final Method m = clazz.getMethod(methodName, methodClazz);
                m.setAccessible(true);
                m.invoke(o, methodArguments);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

}

class OtherClass {
    /**
     * doSomething
     * 
     * @throws InterruptedException
     */
    public void doSomething(final String str1, final String str2)
            throws InterruptedException {
        System.out.println("i am do something, parameter1:" + str1
                + ", parameter2" + str2);

        System.out.println("i am ready to sleep 2 sec..");
        Thread.sleep(2000);
        System.out.println("i has been awaken..");
    }
}
您为什么要join()呢
这里只是更好的说明主线程等待子线程的时间段。即开始和结束,就加上了join了。 不join也是可以的。默认是非守护线程。但是如果是守护线程的话,就要join了。[/quote] public class sss { /** * @param args * @throws InterruptedException */ public static void main(String[] args) throws InterruptedException { final Thread t = new Thread(new Worker("张三", "123456")); t.start(); System.out.println("这里是我接下来要执行的代码"); } static class Worker implements Runnable { private final String userName; private final String userpwd; private Worker(String username, String userpwd) { this.userName = username; this.userpwd = userpwd; } @Override public void run() { //这里我去调用别人的方法 aaa a = new aaa(); String result = a.test(userName); //现在有种情况,这里调用的test()存在调用超时的情况,意思就是对方服务器没有响应 结果 //那么我现在就要做一个调用超时的处理,请问我这里怎么写 //还有上面的代码有什么问题吗? } } }
晓风吹雾 2013-07-26
  • 打赏
  • 举报
回复
引用 9 楼 caibin_caibin 的回复:
[quote=引用 7 楼 caibin_caibin 的回复:] [quote=引用 6 楼 shnulaa 的回复:] 写的一个简单的例子 非同期调用方法

import java.lang.reflect.Method;

public class AsyncCall {

    /**
     * @param args
     * @throws InterruptedException
     */
    public static void main(String[] args) throws InterruptedException {
        // create the thread
        final Thread t = new Thread(new Worker(new Object[] { "string1",
                "string2" }, new Class[] { String.class, String.class },
                OtherClass.class, "doSomething"));
        t.setName("async_call_method_thread");

        System.out.println("ready to call method asynchronous ");
        // call the method asynchronize
        t.start();
        System.out.println("end to call method ");

        System.out.println("wait for the method finish");
        t.join();
        System.out.println("the method has finished");
    }

    static class Worker implements Runnable {
        private final Object[] methodArguments;
        private final Class<?>[] methodClazz;
        private final Class<?> clazz;
        private final String methodName;

        private Worker(Object[] arguments, final Class<?>[] methodClazz,
                final Class<?> clazz, final String methodName) {
            this.methodArguments = arguments;
            this.methodClazz = methodClazz;
            this.clazz = clazz;
            this.methodName = methodName;
        }

        @Override
        public void run() {
            try {
                final Object o = clazz.newInstance();
                final Method m = clazz.getMethod(methodName, methodClazz);
                m.setAccessible(true);
                m.invoke(o, methodArguments);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

}

class OtherClass {
    /**
     * doSomething
     * 
     * @throws InterruptedException
     */
    public void doSomething(final String str1, final String str2)
            throws InterruptedException {
        System.out.println("i am do something, parameter1:" + str1
                + ", parameter2" + str2);

        System.out.println("i am ready to sleep 2 sec..");
        Thread.sleep(2000);
        System.out.println("i has been awaken..");
    }
}
您为什么要join()呢
这里只是更好的说明主线程等待子线程的时间段。即开始和结束,就加上了join了。 不join也是可以的。默认是非守护线程。但是如果是守护线程的话,就要join了。
fedori 2013-07-26
  • 打赏
  • 举报
回复
引用 10 楼 caibin_caibin 的回复:
[quote=引用 8 楼 fedori 的回复:]
public class TestSynchronize {
	public static void main(String[] args) {

		JobOfOther dd = new JobOfOther();
		dd.start();

		for (int i = 0; i < 100; i++) {
			System.out.println("do my job:" + i);
		}

	}
}

class JobOfOther extends Thread {

	public JobOfOther() {

	}

	@Override
	public void run() {
		for (int i = 0; i < 100; i++) {
			System.out.println("do other job:" + i);
		}
	}
}
感谢您的代码,我一直困惑的是我怎么把当前环境中的局部变量给传过去[/quote]
public class TestSynchronize {
	public static void main(String[] args) {

		Object param = new Object();
		JobOfOther dd = new JobOfOther(param);// ㊧param
		dd.start();

		for (int i = 0; i < 5; i++) {
			System.out.println("do my job:" + i+param.hashCode());
		}

	}
}

class JobOfOther extends Thread {

	Object param ;
	public JobOfOther(Object param) { // ㊧param
		this.param = param;
	}

	@Override
	public void run() {
		for (int i = 0; i < 5; i++) {
			System.out.println("do other job:" + i +param.hashCode());
		}
	}
}
caibin_caibin 2013-07-26
  • 打赏
  • 举报
回复
引用 8 楼 fedori 的回复:
public class TestSynchronize {
	public static void main(String[] args) {

		JobOfOther dd = new JobOfOther();
		dd.start();

		for (int i = 0; i < 100; i++) {
			System.out.println("do my job:" + i);
		}

	}
}

class JobOfOther extends Thread {

	public JobOfOther() {

	}

	@Override
	public void run() {
		for (int i = 0; i < 100; i++) {
			System.out.println("do other job:" + i);
		}
	}
}
感谢您的代码,我一直困惑的是我怎么把当前环境中的局部变量给传过去
caibin_caibin 2013-07-26
  • 打赏
  • 举报
回复
[quote=引用 7 楼 caibin_caibin 的回复:] [quote=引用 6 楼 shnulaa 的回复:] 写的一个简单的例子 非同期调用方法

import java.lang.reflect.Method;

public class AsyncCall {

    /**
     * @param args
     * @throws InterruptedException
     */
    public static void main(String[] args) throws InterruptedException {
        // create the thread
        final Thread t = new Thread(new Worker(new Object[] { "string1",
                "string2" }, new Class[] { String.class, String.class },
                OtherClass.class, "doSomething"));
        t.setName("async_call_method_thread");

        System.out.println("ready to call method asynchronous ");
        // call the method asynchronize
        t.start();
        System.out.println("end to call method ");

        System.out.println("wait for the method finish");
        t.join();
        System.out.println("the method has finished");
    }

    static class Worker implements Runnable {
        private final Object[] methodArguments;
        private final Class<?>[] methodClazz;
        private final Class<?> clazz;
        private final String methodName;

        private Worker(Object[] arguments, final Class<?>[] methodClazz,
                final Class<?> clazz, final String methodName) {
            this.methodArguments = arguments;
            this.methodClazz = methodClazz;
            this.clazz = clazz;
            this.methodName = methodName;
        }

        @Override
        public void run() {
            try {
                final Object o = clazz.newInstance();
                final Method m = clazz.getMethod(methodName, methodClazz);
                m.setAccessible(true);
                m.invoke(o, methodArguments);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

}

class OtherClass {
    /**
     * doSomething
     * 
     * @throws InterruptedException
     */
    public void doSomething(final String str1, final String str2)
            throws InterruptedException {
        System.out.println("i am do something, parameter1:" + str1
                + ", parameter2" + str2);

        System.out.println("i am ready to sleep 2 sec..");
        Thread.sleep(2000);
        System.out.println("i has been awaken..");
    }
}
您为什么要join()呢
fedori 2013-07-26
  • 打赏
  • 举报
回复
public class TestSynchronize {
	public static void main(String[] args) {

		JobOfOther dd = new JobOfOther();
		dd.start();

		for (int i = 0; i < 100; i++) {
			System.out.println("do my job:" + i);
		}

	}
}

class JobOfOther extends Thread {

	public JobOfOther() {

	}

	@Override
	public void run() {
		for (int i = 0; i < 100; i++) {
			System.out.println("do other job:" + i);
		}
	}
}
caibin_caibin 2013-07-26
  • 打赏
  • 举报
回复
引用 6 楼 shnulaa 的回复:
写的一个简单的例子 非同期调用方法

import java.lang.reflect.Method;

public class AsyncCall {

    /**
     * @param args
     * @throws InterruptedException
     */
    public static void main(String[] args) throws InterruptedException {
        // create the thread
        final Thread t = new Thread(new Worker(new Object[] { "string1",
                "string2" }, new Class[] { String.class, String.class },
                OtherClass.class, "doSomething"));
        t.setName("async_call_method_thread");

        System.out.println("ready to call method asynchronous ");
        // call the method asynchronize
        t.start();
        System.out.println("end to call method ");

        System.out.println("wait for the method finish");
        t.join();
        System.out.println("the method has finished");
    }

    static class Worker implements Runnable {
        private final Object[] methodArguments;
        private final Class<?>[] methodClazz;
        private final Class<?> clazz;
        private final String methodName;

        private Worker(Object[] arguments, final Class<?>[] methodClazz,
                final Class<?> clazz, final String methodName) {
            this.methodArguments = arguments;
            this.methodClazz = methodClazz;
            this.clazz = clazz;
            this.methodName = methodName;
        }

        @Override
        public void run() {
            try {
                final Object o = clazz.newInstance();
                final Method m = clazz.getMethod(methodName, methodClazz);
                m.setAccessible(true);
                m.invoke(o, methodArguments);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

}

class OtherClass {
    /**
     * doSomething
     * 
     * @throws InterruptedException
     */
    public void doSomething(final String str1, final String str2)
            throws InterruptedException {
        System.out.println("i am do something, parameter1:" + str1
                + ", parameter2" + str2);

        System.out.println("i am ready to sleep 2 sec..");
        Thread.sleep(2000);
        System.out.println("i has been awaken..");
    }
}
感谢您的代码,在下明白了,其实我就是不明白我怎么把当前的形参传过去
晓风吹雾 2013-07-26
  • 打赏
  • 举报
回复
写的一个简单的例子 非同期调用方法

import java.lang.reflect.Method;

public class AsyncCall {

    /**
     * @param args
     * @throws InterruptedException
     */
    public static void main(String[] args) throws InterruptedException {
        // create the thread
        final Thread t = new Thread(new Worker(new Object[] { "string1",
                "string2" }, new Class[] { String.class, String.class },
                OtherClass.class, "doSomething"));
        t.setName("async_call_method_thread");

        System.out.println("ready to call method asynchronous ");
        // call the method asynchronize
        t.start();
        System.out.println("end to call method ");

        System.out.println("wait for the method finish");
        t.join();
        System.out.println("the method has finished");
    }

    static class Worker implements Runnable {
        private final Object[] methodArguments;
        private final Class<?>[] methodClazz;
        private final Class<?> clazz;
        private final String methodName;

        private Worker(Object[] arguments, final Class<?>[] methodClazz,
                final Class<?> clazz, final String methodName) {
            this.methodArguments = arguments;
            this.methodClazz = methodClazz;
            this.clazz = clazz;
            this.methodName = methodName;
        }

        @Override
        public void run() {
            try {
                final Object o = clazz.newInstance();
                final Method m = clazz.getMethod(methodName, methodClazz);
                m.setAccessible(true);
                m.invoke(o, methodArguments);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

}

class OtherClass {
    /**
     * doSomething
     * 
     * @throws InterruptedException
     */
    public void doSomething(final String str1, final String str2)
            throws InterruptedException {
        System.out.println("i am do something, parameter1:" + str1
                + ", parameter2" + str2);

        System.out.println("i am ready to sleep 2 sec..");
        Thread.sleep(2000);
        System.out.println("i has been awaken..");
    }
}
TheoneFx 2013-07-26
  • 打赏
  • 举报
回复
两个线程同步的问题,看你的需求,所谓的“传递参数过去”需要放在一个子线程内执行,过一阵子在查看执行结果。 这个涉及到两个线程同步的问题,上面的就是给你个大概的方向,具体的你还得多查查资料。
caibin_caibin 2013-07-26
  • 打赏
  • 举报
回复
引用 3 楼 splendid_java 的回复:
可以开一个内置线程,查查相关的资料,应该可以满足的
您说的很对,但是。。。。。你懂得
ZHOU西口 2013-07-26
  • 打赏
  • 举报
回复
可以开一个内置线程,查查相关的资料,应该可以满足的
caibin_caibin 2013-07-26
  • 打赏
  • 举报
回复
我的程序是一个Controller里面的一个方法,我取得用户提交的数据,要做一系列的处理,中间就包括把这些数据提给另外一个类中的一个方法来处理,但是,这两个互不干扰,意思就是调用别人的方法不能阻断我的程序执行,
小丑哥_V5 2013-07-26
  • 打赏
  • 举报
回复

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧