设计两个线程,对一个初始值为1的整型变量进行操作,一个线程对变量i进行自加操作,另一个线程对变量i进行自减的操作,要求在程序中变量的值符合010101010。的规则变化java线程面试题设计
解决方案 »
- eclipse有什么快捷键或者使用技巧大家分享下嘛!!
- 多线程问题
- 别人问的题目,答不全面,大家帮帮我。
- 加分帖子! 重赏之下 必定有勇夫! 加100分。
- 关于chart的aesthetic变化的实现问题
- 我们现在做项目主要用SERVLET+JSP+JDBC,请问这些技术过时了吗 ?
- 菜鸟问题~~随意查找数字和字母组合
- 用java.net去post一个请求,但是返回的数据太大,如何完整接受?
- 请问在 Jbuilder6 中 输入提示符与输入错位问题 有什么好的处理方法吗?
- 有关SCJP的问题(广泛收集!有价值的都给分) 谢谢大家!
- 新手求助
- 新人求助啊,为什么我这个paint画出来的线条只闪一下就没有了
其实这个有点类似于生产者、消费者,不过在这里生产者和消费者都只有一个,而且缓冲区的长度也只有1。
1)线程同步;
2)增量时,若已经为1,则使自己睡眠,并释放同步锁;
3)减量时,若已经为0,则使自己睡眠,并释放同步锁。
package test;public class Test implements Runnable {
private static int iValue = 0; private static Object iLock = new Object(); private final int iStepSize; public Test(final int stepSize) {
iStepSize = stepSize;
} @Override
public void run() {
while (true) {
System.out.print(iValue);
iValue += iStepSize;
synchronized (iLock) {
iLock.notifyAll();
try {
iLock.wait();
}
catch (InterruptedException e) { }
}
}
} public static void main(final String[] args) throws Exception {
Thread t1 = new Thread(new Test(1));
Thread t2 = new Thread(new Test(-1));
t1.start();
Thread.sleep(100);
t2.start();
Thread.sleep(500);
}
}
public class Test {
int i = 1; public synchronized void calcNumber() {
if (i != 0) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
i++;
System.out.print(i);
notify();
} public synchronized void calcNumber2() {
if (i != 1) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
i--;
System.out.print(i);
notify();
}
}
public class Thread1 implements Runnable {
Test t; public Thread1(Test t) {
this.t = t;
} @Override
public void run() {
for (int i = 0; i < 10; i++) {
t.calcNumber();
}
}}
public class Thread2 implements Runnable {
Test t; public Thread2(Test t) {
this.t = t;
} @Override
public void run() {
for (int i = 0; i < 10; i++) {
t.calcNumber2();
}
}}
/**
* 测试类
* @author Administrator
*
*/
public class Test2 { /**
* @param args
*/
public static void main(String[] args) {
Test t = new Test();
new Thread(new Thread2(t)).start();
new Thread(new Thread1(t)).start();
}}
public class thread {
private int j=0;
public static void main(String[] args) {
thread incDec=new thread();
Inc inc=incDec.new Inc();
Dec dec=incDec.new Dec();
while(true){
Thread thread=new Thread(inc);
thread.start();
thread=new Thread(dec);
thread.start();
}
}
public synchronized void inc(){
j++;
System.out.println(Thread.currentThread().getName()+"-inc:"+j);
}
public synchronized void dec(){
j--;
System.out.println(Thread.currentThread().getName()+"-dec:"+j);
}
class Inc implements Runnable{
public void run(){
inc();
}
}
class Dec implements Runnable{
public void run(){
dec();
}
}
}
public class thread {
private int j=0;
private static Object iLock = new Object();
public static void main(String[] args) {
thread incDec=new thread();
Inc inc=incDec.new Inc();
Dec dec=incDec.new Dec();
while(true){
Thread thread=new Thread(inc);
thread.start();
thread=new Thread(dec);
thread.start();
}
}
public synchronized void inc(){
j++;
System.out.println(Thread.currentThread().getName()+"-inc:"+j);
}
public synchronized void dec(){
j--;
System.out.println(Thread.currentThread().getName()+"-dec:"+j);
}
class Inc implements Runnable{
public void run(){
synchronized (iLock) {
if(j==0){
j++;
System.out.println(Thread.currentThread().getName()+"-inc:"+j);
iLock.notifyAll();
try {
iLock.wait();
}
catch (InterruptedException e) {
}
}
}
}
}
class Dec implements Runnable{
public void run(){
synchronized (iLock) {
if(j==1){
j--;
System.out.println(Thread.currentThread().getName()+"-dec:"+j);
iLock.notifyAll();
try {
iLock.wait();
}
catch (InterruptedException e) {
}
}
}
}
}
}
public class thread {
private int j=0;
private static Object iLock = new Object();
public static void main(String[] args) {
thread incDec=new thread();
Inc inc=incDec.new Inc();
Dec dec=incDec.new Dec();
while(true){
Thread thread=new Thread(inc);
thread.start();
thread=new Thread(dec);
thread.start();
}
}
class Inc implements Runnable{
public void run(){
synchronized (iLock) {
if(j==0){
j++;
System.out.println(Thread.currentThread().getName()+"-inc:"+j);
iLock.notifyAll();
try {
iLock.wait();
}
catch (InterruptedException e) {
}
}
}
}
}
class Dec implements Runnable{
public void run(){
synchronized (iLock) {
if(j==1){
j--;
System.out.println(Thread.currentThread().getName()+"-dec:"+j);
iLock.notifyAll();
try {
iLock.wait();
}
catch (InterruptedException e) {
}
}
}
}
}
}
输出结果:.............
Thread-17639-dec:0
Thread-17640-inc:1
Thread-17647-dec:0
Thread-17646-inc:1
Thread-17649-dec:0
Thread-17650-inc:1
Thread-17653-dec:0
Thread-17654-inc:1
Thread-17655-dec:0
Thread-17656-inc:1
Thread-17657-dec:0
Thread-17662-inc:1
package com.netviewtech.iot.dapi.client.impl;
public class Test { volatile int i = 1 ; public void start(){ final Object lock = new Object() ; // 自增
new Thread(){
public void run() {
for(int j = 0 ; j < 1000 ; j++){
synchronized (lock) {
try {
lock.wait() ;
} catch (InterruptedException e) {
e.printStackTrace();
}
i++ ;
System.out.print(i);
lock.notify() ;
}
}
};
}.start();
// 自减
new Thread(){
public void run() {
for(int j = 0 ; j < 1000 ; j++){
synchronized (lock) {
i-- ;
System.out.print(i);
lock.notify() ;
try {
lock.wait() ;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
}.start(); } public static void main(String[] args) { new Test().start() ; }
}
1,先有共享资源(就是一个类,属性就是资源,一般有add和delete方法对资源进行操作)。
2,多线程(在这里就是两个线程consumer和producer,实现runnable接口,复写run方法时要进行加锁操作)。
3,主线程(创建线程,并start)。总:就是三个类而已
public class Test
{
public static void main(String[] args) throws Exception{
int i = 1;
Thread t1 = new Thread(new processor(new User(i)));
Thread t2 = new Thread(new processor(new User(i)));
t1.setName("t1");
t2.setName("t2"); t1.start();
Thread.sleep(100);
t2.start();
}
}class processor implements Runnable
{
User u;
processor(User u){
this.u =u;
}
public void run(){
if(Thread.currentThread().getName().equals("t1")){
while(true){
try{User.m1();
System.out.println(Thread.currentThread().getName()+"-->"+u.getU());
Thread.sleep(1000);
}catch(Exception r){}
}
}
if(Thread.currentThread().getName().equals("t2")){
while(true){
try{User.m2();
System.out.println(Thread.currentThread().getName()+"-->"+u.getU());
Thread.sleep(1000);
}catch(Exception r){}
}
}
}
}class User
{ private static int i;
User(int i){
this.i = i;
}
public static void setU(int i){
User.i = i;;
}
public static int getU(){
return i;
}
public synchronized static void m1(){
int a = User.i - 1;
User.setU(a);
}
public synchronized static void m2(){
int a = User.i + 1;
User.setU(a);
}}