「java线程的交替输出」java线程的交替输出是什么
今天给各位分享java线程的交替输出的知识,其中也会对java线程的交替输出是什么进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、编程JAVA 实现使用多线程数字从10到1两个线程的交替输出
- 2、java多线程问题。两个线程交替打印。例如第一个线程打印1,接着第二个线程打印100,接着第一个线程打印2。
- 3、java 多线程编程 交替打印大小写字母的问题
- 4、题1:java 设计一个类,要求两个线程分别交替输出正负整数。下面还有一题
编程JAVA 实现使用多线程数字从10到1两个线程的交替输出
class out implements Runnable
{
private int ticket = 100;
public synchronized void run()
{
while (true)
{
if (ticketo)
{
System.out.println(Thread.currentThread().getName()+"="+"sale..."+ticket--);//若改成this.getName();不行因为接口里没有getName();
}
}
}
}
class RunnableDemo
{
public static void main(String[] args)
{
Ticket P = new Ticket();//先创造接口对象
Thread P1 = new Thread(P);//再把接口对象给Thread
Thread P2 = new Thread(P);
P1.start();
P2.start();
}
这是最基本的。。多线程里面玩的就是同步代码块。。都是手打的。。分给我吧,运行的时候运行RunnableDemo;
java多线程问题。两个线程交替打印。例如第一个线程打印1,接着第二个线程打印100,接着第一个线程打印2。
你这样写两个线程实例t1和t2间没有交互通信,各跑各的,当然不会达到你上面说的那个结果。要想达到你上面说的那个效果,必须进行线程间通信。比如,你可以让两个线程实例都对方的引用,在run函数里执行打印的方法后,就让t2跑,t1去睡觉(sleep())。等t2打印完后,又让t1跑,让t2去睡觉,这样即可。 给你思路,代码我就不写了。
java 多线程编程 交替打印大小写字母的问题
这个就是多线程的一个考察:
public class RunTest {
public static void main(String[] args) {
Myprint mp = new Myprint();
new Capital(mp).start();
new Lowercase(mp).start();
}
}
class Myprint {
boolean flag = true;
int i = 0;
int j = 0;
public void Da() {
while (i 26) {
if (flag) {
System.out.print((char) ('A' + i));
i++;
this.flag = false;
}
}
}
public void Xiao() {
while (j 26) {
if (!flag) {
System.out.print((char) ('a' + j));
j++;
this.flag = true;
}
}
}
}
class Capital extends Thread {
Myprint my = null;
public Capital(Myprint my) {
this.my = my;
}
@Override
public void run() {
my.Da();//打印大写
}
}
class Lowercase extends Thread {
Myprint my = null;
public Lowercase(Myprint my) {
this.my = my;
}
@Override
public void run() {
my.Xiao();//打印小写
}
}
题1:java 设计一个类,要求两个线程分别交替输出正负整数。下面还有一题
1、
public class PrintNumber implements Runnable
{
private int sign = 1;//1输出正数,-1输出负数
private static int seg = 1; //同步信号, 全局常量,所有线程共享
public PrintNumber(int sign) {
this.sign = sign;
}
public void run() {
for (int i=1; i10; i++)
{
try
{
printNumber(i);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
//同步方法
private synchronized void printNumber(int i) throws InterruptedException{
while (seg != sign)
{
wait(10);
}
System.out.println(sign * i);
seg = -1 * seg;
}
public static void main(String[] args) {
PrintNumber p1 = new PrintNumber(1);
PrintNumber p2 = new PrintNumber(-1);
new Thread(p1).start();
new Thread(p2).start();
}
}
2、
public class Reduction implements Runnable
{
private String name; //线程名字
private int count = 100;
public Reduction(String name) {
this.name = name;
}
public void run() {
while (count 0)
{
int rand = (int)(Math.random() * 10);
count = count - rand;
}
System.out.println(name + " at " + new java.util.Date() + " finished!");
}
public static void main(String[] args)
{
Reduction p1 = new Reduction("P1");
Reduction p2 = new Reduction("P2");
new Thread(p1).start();
new Thread(p2).start();
}
}
java线程的交替输出的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java线程的交替输出是什么、java线程的交替输出的信息别忘了在本站进行查找喔。