Witam,
Wątki w Javie można tworzyć na dwa sposoby. Pierwszy z nich to zaimplementować interfejs Runnable i dodać do klasy metodę run, bądź dziedziczyć po klasie Thread i zrobić overriding na metodzie run.
Stworzyłem sobie taką klasę na funkcję main:
package com.user.threadtest;
public class ThreadTest {
public static void main(String[] args) {
StringThread str = new StringThread("dupa", 4);
}
}
Oraz klasę na mój wątek:
package com.user.threadtest;
class StringThread implements Runnable
{
private String str;
private int num;
StringThread(String s, int n)
{
str = new String (s);
num =n;
}
public void run ( )
{
for (int i=1; i<=num; i++)
System.out.print (str+" ");
}
}
Pytanie:
Mogę co najwyżej str.run(). Ale nie tworzy to wątku, po prostu wykonuje metodę. Oprócz tego dziwi mnie, że w Javie metoda run() w implementowanym interfejsie jest publiczna skoro wątek uruchamia się metodą start.
Problem:
Dlaczego nie mogę wykonać str.start()?
Description Resource Path Location Type
The method start() is undefined for the type StringThread ThreadTest.java /ThreadTest/src/com/user/threadtest line 6 Java Problem