package scjp;
public class Demo67 implements Runnable
{
String myString = "Yes ";
public void run()
{
this.myString = "No ";
}
public static void main(String[] args)
{
Demo67 t = new Demo67();
Thread thread=new Thread(t);
System.out.println(thread.getId());
System.out.println(Thread.currentThread().getId());
for (int i=0; i < 10; i++)
System.out.print(t.myString);
}
}
打印结果是不确定的,为什么呢?是因为有两个线程一个是t,一个main里启动的主线程:
The 'main()' method in Java is referred to the thread that is running, whenever a Java program runs. It calls the main thread because it is the first thread that starts running when a program begins. Other threads can be spawned from this main thread. The main thread must be the last thread in the program to end. When the main thread stops, the program stops running.
Main thread is created automatically, but it can be controlled by the program by using a Thread object. The Thread object will hold the reference of the main thread with the help of currentThread() method of the Thread class.
--翻译
main()方法代表了正在运行的线程,因为任何Java程序的执行都首先生成main线程,其它线程都由其衍生,并且它是最后一个在程序中执行的线程,一旦main线程停止程序也就停止执行。
main线程是自动产生的,它可以被一个获得了main线程“引用”的 Thread 对象控制。这个方法是Thread.currentThread.
class MainThread
{
public static void main(String args [] )
{
Thread t = Thread.currentThread ( );
System.out.println ("Current Thread : " + t);
System.out.println ("Name : " + t.getName ( ) );
System.out.println (" ");
t.setName ("New Thread");
System.out.println ("After changing name");
System.out.println ("Current Thread : " + t);
System.out.println ("Name : " + t.getName ( ) );
System.out.println (" ");
System.out.println ("This thread prints first 10 numbers");
try
{
for (int i=1; i<=10;i++)
{
System.out.print(i);
System.out.print(" ");
Thread.sleep(1000);
}
}
catch (InterruptedException e)
{
System.out.println(e);
}
}
}
分享到:
相关推荐
Exception in thread “main“ java.util.InputMismatchException
出现Exception in threadmain java.lang.NoClassDefFoundError的各种可能情况.doc
AndroidChannel is helper library for inter thread communication between main thread and worker thread. AndroidChannel uses HandlerThread for inter thread communication. Setup Gradle dependencies { .....
* 在java中真正开启线程的方法是这样的。 */ Thread t = new Thread(r); t.start(); setContentView(R.layout.activity_main); System.out.println("Activity-->"+ Thread.currentThread().getId()...
主要介绍了Exception in thread main java.lang.NoClassDefFoundError错误解决方法,需要的朋友可以参考下
Exception in thread main java.lang.UnsatisfiedLinkError解决
NULL 博文链接:https://utopialxw.iteye.com/blog/1138133
$ npm install --save @surma/rollup-plugin-off-main-thread 工作者是JavaScript的线程版本。 因为主线程已经超载,尤其是在较慢或较旧的设备上。 该插件负责辅助worker中的填充模块,并允许您使用new Worker() ...
RT-Thread Studio与CubeMX联合编程例程 与改文章配套: https://blog.csdn.net/qq_40824852/article/details/123067421
Forum thread : Unity Forums Note : You can also buy Easy Main Menu from Sellfy Store! Sellfy Store ------------------------------------------------ A Humble Request : Please guys, to support...
Qt5中两种线程操作的方法 1. 通过创建QTread子实现run()函数来实现 QThread::run()是线程的入口 相当于 main函数一样 创建的线程通过调用start()来执行run(); run() 是一个虚函数 通过子类实现方法 通过moveToThread...
main 这种方法 在管理控制台中按下ctrl + break 就能从界面上获得及时的堆栈信息了,但是这些信息很不方便。那么又如何能不在 dos窗口中运行,而是在eclipse集成环境中,并且还能使其写入文件,最后还能用可视化...
java视频。关于thread,让大家很好的理解thread线程
当NewThread调用Join方法的时候,MainThread就被停止执行, 直到NewThread线程执行完毕。 Thread oThread = new Thread(new ThreadStart(oAlpha.Beta)); oThread.Start(); while (!oThread.IsAlive) //表示线程当
} } public class ThreadPriority { public static void main(String[] args) { Thread thread1=new Thread1(); Thread thread2=new Thread2(); thread1.setPriority(thread.MIN_PRIORITY); thread1.setPriority...
希望高手指点
/*join的意思是使得放弃当前线程的执行,并返回对应的线程,例如下面代码的意思就是:程序在main线程中调用t1线程的join方法,则main线程放弃cpu控
在main()方法中启动两个线程,然后调用thread1.join()和thread2.join(),以使主线程在“线程1”和“线程2”都返回结果之前不会进一步执行。 最后将两个线程的计算结果相加,得到100! + 100000! 下面就让我们使用...
将 CubeMx 生成的代码 stm32f7xx_hal_msp.c 函数复制到 RT-Thread Studio 生成的工程中,并参与工程编译: 由于我们并没有使用 CubeMx 生成的工程,所以这里需要将 stm32f7xx_hal_msp.c 文件中 #include “main.h” ...