问题描述 : -
第1步:在主线程中从用户处输入FILE_NAME.
步骤2: 对该文件执行10次操作(即计数字符,计数行等),所有这10个操作必须在隔离线程中.这意味着必须有10个子线程.
第3步:主线程等待所有子线程完成.
第4步:打印结果.
我做了什么 :-
我做了3个线程的示例代码.我不希望你身边的文件操作代码.
public class ThreadTest {
// This is object to synchronize on.
private static final Object waitObject = ThreadTest.class;
// Your boolean.
private static boolean boolValue = false;
public final Result result = new Result();
public static void main(String[] args) {
final ThreadTest mytest = new ThreadTest();
System.out.println("main started");
new Thread(new Runnable() {
public void run() {
System.out.println("Inside thread");
//Int initialiser
new Thread(new Runnable() {
public …Run Code Online (Sandbox Code Playgroud) 总的来说,有些事情我无法理解。就我而言,使用Java和Android。
假设我有一个名为Thread的线程A,它将启动thread B。如果线程A停止,则线程B将继续运行。这怎么可能?谁B现在属于线程?到主线程?
线程类
public class ParentThread extends Thread {
public void run(){
Log.e("PARENT THREAD", "STARTS RUNNING");
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
Log.e("CHILD THREAD", "IS ALIVE");
try {
Thread.sleep(1000);
}
catch (InterruptedException exc) {
e.printStackTrace();
}
}
}
}).start();
Log.e("PARENT THREAD", "STOPS RUNNING");
}
}
Run Code Online (Sandbox Code Playgroud)
活动
new ParentThread().start();
Run Code Online (Sandbox Code Playgroud)
Logcat输出
public class ParentThread extends Thread {
public void run(){
Log.e("PARENT THREAD", "STARTS RUNNING");
new Thread(new …Run Code Online (Sandbox Code Playgroud)