这段代码工作正常但如果我在第6行使用构造函数Thread(name)而不是Thread(this,name)它不起作用我只是想知道是什么产生了区别?
public class threadtest implements Runnable{
Thread t;
public threadtest(String name)
{
System.out.println("satheesh");
Thread t=new Thread(this,name);
t.start();
t=null;
//System.out.println(this+"\n"+t);
}
public void run(){
System.out.println("satheesh");
for(int i=0;i<=10;i++)
{
try{
System.out.println("satheesh");
Thread.sleep(1000);
System.out.print(Thread.currentThread());
}
catch(Exception e)
{
System.out.println(e);
}
}
}
public static void main(String args[])
{
threadtest ob=new threadtest("satheesh");
}
}
Run Code Online (Sandbox Code Playgroud) 我有两个关于多线程编程的问题.我在网上看了几个答案,但我仍然找不到令人满意的答案.
实现Runnable比扩展线程类更受欢迎.为什么?
我们如何能够通过覆盖run()方法来逃脱?
根据Herbert Schildt的"The Complete Reference to Java",如果我们没有覆盖除run()之外的Thread类的任何方法,那么我们最好实现Runnable.
我的第二个问题可能听起来有点傻,但我似乎错过了一些东西,我不确定整个事情是如何运作的.
任何人都可以在我们扩展Thread类时以及在java中实现runnable接口时解释.什么时候我们必须使用这些概念?找到确切答案我感觉有点棘手.
两者之间有区别吗?
class MyThread extends Thread
{
}
MyThread obj = new MyThread();
Thread t = new Thread(obj);
t.start()
Run Code Online (Sandbox Code Playgroud)
VS
obj.start()
Run Code Online (Sandbox Code Playgroud)
选择一个优于其他优势是否有任何优势?
嗨,我做了一个扩展线程的东西,添加添加一个具有IP的对象.然后我做了这个线程的两个实例并启动它们.他们使用相同的列表.
我现在想使用Synchronized来停止并发更新问题.但它没有工作,我无法解决原因.
我的主要课程:
import java.util.*;
import java.io.*;
import java.net.*;
class ListTest2 {
public static LinkedList<Peer> myList = new LinkedList<Peer>();
public static void main(String [] args) {
try {
AddIp test1 = new AddIp(myList);
AddIp test2 = new AddIp(myList);
test1.start();
test2.start();
} catch(Exception e) {
System.out.println("not working");
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的线程类:
class AddIp extends Thread {
public static int startIp = 0;
List<Peer> myList;
public AddIp(List<Peer> l) {
myList = l;
}
public synchronized void run() {
try {
startIp = …Run Code Online (Sandbox Code Playgroud) 我在String数组上有一个简单的循环,然后将String传递给threadlist方法.但是我似乎无法打印出两个String.它只打印第二个名称"Fred",这让我觉得我用第二个String覆盖了第一个String.我怎样才能ArrayList包括字符串"Tim"和"Fred"?
import java.util.ArrayList;
public class Threads extends Thread implements Runnable{
private ArrayList threadList;
private String e;
public static void main(String[] args) {
String[] elements = {"Tim","Fred"};
Threads t = new Threads();
for (String e: elements) {
t.threadL(e);
}
//loop over the elements of the String array and on each loop pass the String to threadL
for (int index = 0;index<t.threadList.size();index++){
System.out.print(t.threadList.get(index));
}
//loop over the threadList arraylist and printout
}
public ArrayList<String> …Run Code Online (Sandbox Code Playgroud) 有人可以解释这段代码的作用吗? new Thread(new X()).start();
其余代码:
class X implements Runnable {
X() {}
}
public static void main(String[] arg) {
new Thread(new X()).start();
}
}
Run Code Online (Sandbox Code Playgroud) 通常线程可以通过两种方式创建
通过阅读所有教程,所有人都说实现 Runnable 接口总是好的,因为如果您通过扩展(第一种方法)创建,则无法扩展任何其他类,我同意。而且如果我们通过扩展创建线程,那么每个线程都会创建新对象,而如果我们通过实现所有线程创建线程将共享相同的对象。
我的问题是,如果实现 Runnable 是最好的选择,而扩展线程是不好的做法,为什么 java 有这个选项?我相信如果我们通过扩展创建线程肯定会有一些优势(我相信我们应该使用扩展线程方法,当没有其他父类不是唯一的答案时)
提前致谢
我想知道如何通过"扩展Thread类"来创建多个线程.我知道如何使用"Runnable"完成它.请告诉我如何通过"扩展Thread类"来完成它.